Friday, April 10, 2015

ArrayList, LinkedList, and Vector

ArrayList, LinkedList, and Vector are all implementations of the List interface.

ArrayList and Vector each use an dynamic array to store the elements of the list. Bother Vector and ArrayList are derived from AbstractList and implements List interface, which means both of them are ordered collection and allows duplicates. Another similarity between Vector vs ArrayList is that both are index based Collection and you can use get(index) method to retrieve objects from Vector and ArrayList. When an element is inserted into (or removed from) the middle of the list, the elements that follow must all be shifted accordingly. Vector is synchronized, so if a thread-safe implementation is not needed, it is recommended to use ArrayList rather than Vector.There is a different in Data Growth method for ArrayList and Vector also. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.


LinkedList, on the other hand, is implemented using a doubly linked list. As a result, an inserting or removing an element only requires updating the links that immediately precede and follow the element being inserted or removed.


Among them, LinkedList is generally going to give you the best performance.

However, it is worth noting that if performance is that critical, it’s better to just use an array and manage it yourself, or use one of the high performance 3rd party packages such as Trove or HPPC.

No comments: