The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The core collection interfaces encapsulate different types of collections, which are shown in the figure below. 核心集合接口封装了不同类型的集合,如下图所示。These interfaces allow collections to be manipulated independently of the details of their representation. 这些接口允许独立于集合表示的细节来操作集合。Core collection interfaces are the foundation of the Java Collections Framework. 核心集合接口是java集合框架的基础。As you can see in the following figure, the core collection interfaces form a hierarchy.如下图所示,核心集合接口构成了一个层次结构。
The core collection interfaces.核心集合接口。
A Set
is a special kind of Collection
, a SortedSet
is a special kind of Set
, and so forth. Set
是一种特殊的Collection
,SortedSet
是一种特殊的Set
,依此类推。Note also that the hierarchy consists of two distinct trees a 还要注意,层次结构由两个不同的树组成Map
is not a true Collection
.Map
不是真实的Collection
。
Note that all the core collection interfaces are generic. 请注意,所有核心集合接口都是通用的。For example, this is the declaration of the 例如,这是Collection
interface.Collection
接口的声明。
public interface Collection<E>...
The 这个<E>
syntax tells you that the interface is generic. <E>
语法告诉您接口是通用的。When you declare a 声明Collection
instance you can and should specify the type of object contained in the collection. Collection
实例时,可以并且应该指定集合中包含的对象的类型。Specifying the type allows the compiler to verify (at compile-time) that the type of object you put into the collection is correct, thus reducing errors at runtime. 指定类型允许编译器(在编译时)验证放入集合的对象的类型是否正确,从而减少运行时的错误。For information on generic types, see the Generics (Updated) lesson.有关泛型类型的信息,请参阅泛型(已更新)课程。
When you understand how to use these interfaces, you will know most of what there is to know about the Java Collections Framework. 当您了解如何使用这些接口时,您将了解Java集合框架的大部分内容。This chapter discusses general guidelines for effective use of the interfaces, including when to use which interface. 本章讨论有效使用接口的一般准则,包括何时使用哪个接口。You'll also learn programming idioms for each interface to help you get the most out of it.您还将学习每个接口的编程习惯用法,以帮助您充分利用它。
To keep the number of core collection interfaces manageable, the Java platform doesn't provide separate interfaces for each variant of each collection type. 为了保持核心集合接口的数量可管理,Java平台没有为每个集合类型的每个变体提供单独的接口。(Such variants might include immutable, fixed-size, and append-only.) (此类变体可能包括不可变、固定大小和仅附加。)Instead, the modification operations in each interface are designated optional a given implementation may elect not to support all operations. 相反,每个接口中的修改操作被指定为可选的给定的实现可能选择不支持所有操作。If an unsupported operation is invoked, a collection throws an 如果调用了不受支持的操作,则集合将抛出UnsupportedOperationException
. UnsupportedOperationException
。Implementations are responsible for documenting which of the optional operations they support. 实现负责记录它们支持哪些可选操作。All of the Java platform's general-purpose implementations support all of the optional operations.Java平台的所有通用实现都支持所有可选操作。
The following list describes the core collection interfaces:以下列表描述了核心集合接口:
Collection
Collection
interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired. Collection
接口是所有集合实现的最小公分母,用于传递集合,并在需要最大通用性时对其进行操作。Set
and List
. Set
和List
。Set
List
List
s can contain duplicate elements. List
可以包含重复的元素。List
generally has precise control over where in the list each element is inserted and can access elements by their integer index (position). List
的用户通常可以精确控制每个元素在列表中的插入位置,并可以通过其整数索引(位置)访问元素。Vector
, you're familiar with the general flavor of List
. Vector
,那么您熟悉List
的一般风格。Queue
Collection
operations, a Queue
provides additional insertion, extraction, and inspection operations. Collection
操作外,Queue
还提供附加的插入、提取和检查操作。Queues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner. 队列通常(但不一定)以FIFO(先进先出)的方式对元素进行排序。Among the exceptions are priority queues, which order elements according to a supplied comparator or the elements' natural ordering. 例外情况包括优先级队列,它根据提供的比较器或元素的自然顺序对元素进行排序。Whatever the ordering used, the head of the queue is the element that would be removed by a call to 无论使用何种顺序,队列的头部都是通过调用remove
or poll
. remove
或poll
来移除的元素。In a FIFO queue, all new elements are inserted at the tail of the queue. 在FIFO队列中,所有新元素都插入到队列的尾部。Other kinds of queues may use different placement rules. 其他类型的队列可能使用不同的放置规则。Every 每个Queue
implementation must specify its ordering properties. Queue
实现都必须指定其排序属性。Also see The Queue Interface section.另请参见Queue接口部分。
Deque
Collection
operations, a Deque
provides additional insertion, extraction, and inspection operations. Collection
操作外,Deque
还提供附加的插入、提取和检查操作。Deques can be used both as FIFO (first-in, first-out) and LIFO (last-in, first-out). Deque
既可以用作先进先出(FIFO)也可以用作后进先出(LIFO)。In a deque all new elements can be inserted, retrieved and removed at both ends. 在deque中,可以在两端插入、检索和删除所有新元素。Also see The Deque Interface section.另请参见Deque接口部分。
Map
Map
cannot contain duplicate keys; each key can map to at most one value. Map
不能包含重复的键;每个键最多可以映射到一个值。Hashtable
, you're already familiar with the basics of Map
. Hashtable
,那么您已经熟悉Map
的基础知识了。The last two core collection interfaces are merely sorted versions of 最后两个核心集合接口仅为Set
and Map
:Set
和Map
的排序版本:
SortedSet
Set
that maintains its elements in ascending order. Set
。SortedMap
Map
that maintains its mappings in ascending key order. Map
。Map
analog of SortedSet
. SortedSet
的Map
模拟。To understand how the sorted interfaces maintain the order of their elements, see the Object Ordering section.要了解已排序的接口如何维护其元素的顺序,请参阅对象排序部分。