Documentation

The Java™ Tutorials
Hide TOC
List Implementations实现
Trail: Collections
Lesson: Implementations

List Implementations实现

List implementations are grouped into general-purpose and special-purpose implementations.实现分为通用和专用实现。

General-Purpose List Implementations通用列表实现

There are two general-purpose List implementations — ArrayList and LinkedList. 有两种通用List实现—ArrayListLinkedListMost of the time, you'll probably use ArrayList, which offers constant-time positional access and is just plain fast. 大多数情况下,您可能会使用ArrayList,它提供了恒定时间的位置访问,而且速度非常快。It does not have to allocate a node object for each element in the List, and it can take advantage of System.arraycopy when it has to move multiple elements at the same time. 它不必为List中的每个元素分配一个节点对象,当它必须同时移动多个元素时,它可以利用System.arraycopyThink of ArrayList as Vector without the synchronization overhead.ArrayList视为没有同步开销的Vector

If you frequently add elements to the beginning of the List or iterate over the List to delete elements from its interior, you should consider using LinkedList. 如果您经常将元素添加到List的开头,或在List中迭代以从列表内部删除元素,则应考虑使用LinkedListThese operations require constant-time in a LinkedList and linear-time in an ArrayList. But you pay a big price in performance. 这些操作需要LinkedList中的恒定时间和ArrayList中的线性时间。但你在表现上付出了巨大的代价。Positional access requires linear-time in a LinkedList and constant-time in an ArrayList. 位置访问在LinkedList中需要线性时间,在ArrayList中需要恒定时间。Furthermore, the constant factor for LinkedList is much worse. 此外,LinkedList的常量因子要差得多。If you think you want to use a LinkedList, measure the performance of your application with both LinkedList and ArrayList before making your choice; ArrayList is usually faster.如果您认为要使用LinkedList,请在做出选择之前,使用LinkedListArrayList测量应用程序的性能;ArrayList通常更快。

ArrayList has one tuning parameter — the initial capacity, which refers to the number of elements the ArrayList can hold before it has to grow. 具有一个调谐参数—初始容量,指ArrayList在必须增长之前可以容纳的元素数。LinkedList has no tuning parameters and seven optional operations, one of which is clone. 没有调优参数和七个可选操作,其中一个是cloneThe other six are addFirst, getFirst, removeFirst, addLast, getLast, and removeLast. 其他六个是addFirstgetFirstremoveFirstaddLastgetLastremoveLastLinkedList also implements the Queue interface.LinkedList还实现了Queue接口。

Special-Purpose List Implementations特殊用途列表实现

CopyOnWriteArrayList is a List implementation backed up by a copy-on-write array. 是由写时复制阵列备份的List实现。This implementation is similar in nature to CopyOnWriteArraySet. 此实现在本质上类似于CopyOnWriteArraySetNo synchronization is necessary, even during iteration, and iterators are guaranteed never to throw ConcurrentModificationException. 即使在迭代期间,也不需要同步,并且保证迭代器永远不会抛出ConcurrentModificationExceptionThis implementation is well suited to maintaining event-handler lists, in which change is infrequent, and traversal is frequent and potentially time-consuming.这种实现非常适合于维护事件处理程序列表,其中更改很少,遍历频繁且可能耗时。

If you need synchronization, a Vector will be slightly faster than an ArrayList synchronized with Collections.synchronizedList. 如果需要同步,Vector将略快于与Collections.synchronizedList同步的ArrayListBut Vector has loads of legacy operations, so be careful to always manipulate the Vector with the List interface or else you won't be able to replace the implementation at a later time.但是Vector有大量的遗留操作,所以要小心始终使用List接口来操作Vector,否则以后将无法替换实现。

If your List is fixed in size — that is, you'll never use remove, add, or any of the bulk operations other than containsAll — you have a third option that's definitely worth considering. 如果您的List大小固定—也就是说,除了containsAll—,您永远不会使用removeadd或任何批量操作;你有第三个选择绝对值得考虑。See Arrays.asList in the Convenience Implementations section for more information.有关详细信息,请参阅方便的实现部分中的Arrays.asList


Previous page: Set Implementations
Next page: Map Implementations