Listimplementations are grouped into general-purpose and special-purpose implementations.General-Purpose List Implementations
There are two general-purposeListimplementations ArrayListandLinkedList. Most of the time, you'll probably useArrayList, which offers constant-time positional access and is just plain fast. It does not have to allocate a node object for each element in theList, and it can take advantage ofSystem.arraycopywhen it has to move multiple elements at the same time. Think ofArrayListasVectorwithout the synchronization overhead.If you frequently add elements to the beginning of the
Listor iterate over theListto delete elements from its interior, you should consider usingLinkedList. These operations require constant-time in aLinkedListand linear-time in anArrayList. But you pay a big price in performance. Positional access requires linear-time in aLinkedListand constant-time in anArrayList. Furthermore, the constant factor forLinkedListis much worse. If you think you want to use aLinkedList, measure the performance of your application with bothLinkedListandArrayListbefore making your choice;ArrayListis usually faster.
ArrayListhas one tuning parameter the initial capacity, which refers to the number of elements theArrayListcan hold before it has to grow.LinkedListhas no tuning parameters and seven optional operations, one of which isclone. The other six areaddFirst,getFirst,removeFirst,addLast,getLast, andremoveLast.LinkedListalso implements theQueueinterface.Special-Purpose List Implementations
CopyOnWriteArrayListis aListimplementation backed up by a copy-on-write array. This implementation is similar in nature toCopyOnWriteArraySet. No synchronization is necessary, even during iteration, and iterators are guaranteed never to throwConcurrentModificationException. This 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
Vectorwill be slightly faster than anArrayListsynchronized withCollections.synchronizedList. ButVectorhas loads of legacy operations, so be careful to always manipulate theVectorwith theListinterface or else you won't be able to replace the implementation at a later time.If your
Listis fixed in size that is, you'll never useremove,add, or any of the bulk operations other thancontainsAll you have a third option that's definitely worth considering. SeeArrays.asListin the Convenience Implementations section for more information.