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发行说明。
Implementations are the data objects used to store collections, which implement the interfaces described in the Interfaces section. 实现是用于存储集合的数据对象,这些集合实现了接口部分中描述的接口。This lesson describes the following kinds of implementations:本课程介绍了以下几种实现:
java.util.concurrent
package.java.util.concurrent
包的一部分。The general-purpose implementations are summarized in the following table.下表总结了通用实现。
Set |
HashSet |
TreeSet |
LinkedHashSet | ||
List |
ArrayList |
LinkedList |
|||
Queue |
|||||
Deque |
ArrayDeque |
LinkedList |
|||
Map |
HashMap |
TreeMap |
LinkedHashMap |
As you can see from the table, the Java Collections Framework provides several general-purpose implementations of the 从表中可以看出,Java Collections框架提供了Set
, List
, and Map
interfaces. Set
、List
和Map
接口的几种通用实现。In each case, one implementation 在每种情况下,一个实现HashSet
, ArrayList
, and HashMap
is clearly the one to use for most applications, all other things being equal. HashSet
、ArrayList
和HashMap
显然,在所有其他条件相同的情况下,它适用于大多数应用程序。Note that the 请注意,SortedSet
and the SortedMap
interfaces do not have rows in the table. SortedSet
和SortedMap
接口在表中没有行。Each of those interfaces has one implementation (每个接口都有一个实现((TreeSet
and TreeMap
) and is listed in the Set
and the Map
rows. TreeSet
和TreeMap
),并在集合和映射行中列出。There are two general-purpose 有两种通用Queue
implementations LinkedList
, which is also a List
implementation, and PriorityQueue
, which is omitted from the table. Queue
实现LinkedList
也是一个List
实现,PriorityQueue
则从表中省略。These two implementations provide very different semantics: 这两种实现提供了非常不同的语义:LinkedList
provides FIFO semantics, while PriorityQueue
orders its elements according to their values.LinkedList
提供FIFO语义,而PriorityQueue
根据元素的值对其元素进行排序。
Each of the general-purpose implementations provides all optional operations contained in its interface. 每个通用实现都提供其接口中包含的所有可选操作。All permit 所有这些都允许null
elements, keys, and values. null
元素、键和值。None are synchronized (thread-safe). 没有一个是同步的(线程安全的)。All have fail-fast iterators, which detect illegal concurrent modification during iteration and fail quickly and cleanly rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. 它们都有快速失效的迭代器,可以在迭代过程中检测到非法的并发修改,并快速、干净地失效,而不是在未来的不确定时间冒着任意、不确定性行为的风险。All are 它们都是Serializable
and all support a public clone
method.Serializable
(可序列化的),都支持公共clone
方法。
The fact that these implementations are unsynchronized represents a break with the past: The legacy collections 这些实现是不同步的,这代表着与过去的决裂:遗留集合Vector
and Hashtable
are synchronized. Vector
和Hashtable
是同步的。The present approach was taken because collections are frequently used when the synchronization is of no benefit. 之所以采用目前的方法,是因为在同步毫无益处的情况下经常使用集合。Such uses include single-threaded use, read-only use, and use as part of a larger data object that does its own synchronization. 这类使用包括单线程使用、只读使用,以及作为更大的数据对象的一部分使用,该数据对象自行进行同步。In general, it is good API design practice not to make users pay for a feature they don't use. 一般来说,不让用户为他们不使用的功能付费是良好的API设计实践。Furthermore, unnecessary synchronization can result in deadlock under certain circumstances.此外,在某些情况下,不必要的同步可能会导致死锁。
If you need thread-safe collections, the synchronization wrappers, described in the Wrapper Implementations section, allow any collection to be transformed into a synchronized collection. 如果需要线程安全的集合,那么包装器实现部分中描述的同步包装器允许将任何集合转换为同步集合。Thus, synchronization is optional for general-purpose implementations, whereas it is mandatory for legacy implementations. 因此,同步对于通用实现是可选的,而对于遗留实现是强制性的。Moreover, the 此外,java.util.concurrent
package provides concurrent implementations of the BlockingQueue
interface, which extends Queue
, and of the ConcurrentMap
interface, which extends Map
. java.util.concurrent
包提供了BlockingQueue
接口和ConcurrentMap
接口的并发实现,前者扩展了队列,后者扩展了映射。These implementations offer much higher concurrency than mere synchronized implementations.这些实现比单纯的同步实现提供了更高的并发性。
As a rule, you should be thinking about the interfaces, not the implementations. 通常,您应该考虑接口,而不是实现。That is why there are no programming examples in this section. 这就是为什么本节中没有编程示例的原因。For the most part, the choice of implementation affects only performance. 在大多数情况下,实现的选择只影响性能。The preferred style, as mentioned in the Interfaces section, is to choose an implementation when a 如接口部分所述,首选的样式是在创建Collection
is created and to immediately assign the new collection to a variable of the corresponding interface type (or to pass the collection to a method expecting an argument of the interface type). Collection
时选择一个实现,并立即将新集合分配给相应接口类型的变量(或将集合传递给需要接口类型参数的方法)。In this way, the program does not become dependent on any added methods in a given implementation, leaving the programmer free to change implementations anytime that it is warranted by performance concerns or behavioral details.通过这种方式,程序不会依赖于给定实现中添加的任何方法,让程序员可以随时根据性能问题或行为细节更改实现。
The sections that follow briefly discuss the implementations. 接下来的几节将简要讨论这些实现。The performance of the implementations is described using words such as constant-time, log, linear, n log(n), and quadratic to refer to the asymptotic upper-bound on the time complexity of performing the operation. 使用诸如常数时间、对数、线性、n log(n)和二次等词来描述实现的性能,以表示执行操作的时间复杂度的渐近上界。All this is quite a mouthful, and it doesn't matter much if you don't know what it means. 所有这些都是满嘴的,如果你不知道这意味着什么,那也没什么大不了的。If you're interested in knowing more, refer to any good algorithms textbook. One thing to keep in mind is that this sort of performance metric has its limitations. Sometimes, the nominally slower implementation may be faster. 如果你想了解更多,可以参考任何好的算法教科书。需要记住的一点是,这种性能指标有其局限性。有时,名义上较慢的实现可能更快。When in doubt, measure the performance!如果有疑问,请衡量绩效!