Hello Friends,
This is part III for java collections tutorial. Earlier we have discussed regarding SET collection interface in part II. In this, we will be focusing on an interface that is used when the sequence of the elements needs to be maintained, yes that’s right its LIST interface. List interface in Java Collection Framework also extends the base interface, Collection.
List interface extends the Collection Interface itself. The List interface has precise control over where each element is inserted. One can access elements by their integer index (position), and search for elements in the list. Unlike Set interface, it allows duplicates
The List interface is further implemented by the classes Vectors, ArrayList and LinkedList. We will see each of these implementations in details.
1) ArrayList : It is an resizable-array implementation of the List interface. It implements all the operations of List, and permits all elements, including null. This class provides methods to manipulate the size of the array that is used internally to store the list. By default, all the new elements that are added to arraylist are added to last position, else you can mention the position(index). As elements are added to an ArrayList, its capacity grows automatically by half of its original size. The most important point with ArrayList is that it is not Synchronized.
2) LinkedList : It is the Linked List implementation of List Interface. It also implements all the operations of List and also permits null. the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning or end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue (deque). Like ArrayList, its implementation is also not synchronized.
3) Vectors : Vectors class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow its size to double when element insertion reaches at last. Its implementation is very much similar to ArrayList, only change is that Vectors are Synchronized. And since it is synchronized, it is slower that ArrayList, but is Thread safe.
Hope this will help you.
Thanks,
Nikhil Naoghare