Tuesday 11 October 2016

List Interface - An Interview based approach.

List is all about indexes and index based access of elements. The one thing that List has that non-lists don't have is a set of methods related to the index. Those key methods include things like get(int index) , indexOf(Object o) , add(int index, Object obj) , and so on. The three List implementations are ordered by index position—a position that you determine either by setting an object at a specific index or by adding it without specifying position, in which case the object is added to the end. The three List implementations are described in the following sections.

ArrayList : ArrayList can be considered as dynamically resizing array. It gives you fast iteration and fast random access. To state the obvious: it is an ordered collection (by index), but not sorted. It implements RandomAccess interface—a marker interface that says, "this list supports fast (generally constant time) random access.

Vector : Vector is a holdover from the earliest days of Java; Vector and Hashtable were the two original collections, the rest were added with Java 2 versions 1.2 and 1.4. A Vector is basically the same as an ArrayList, but Vector methods are synchronized for thread safety. You'll normally want to use ArrayList instead of Vector because the synchronized methods add a performance hit you might not need. And if you do need thread safety, there are utility methods in class Collections that can help. Vector is the only class other than ArrayList to implement RandomAccess.

LinkedList A LinkedList is ordered by index position, like ArrayList, except that the elements are doubly-linked to one another. This linkage gives you new methods (beyond what you get from the List interface) for adding and removing from the beginning or end, which makes it an easy choice for implementing a  stack or queue. Remenber that a LinkedList may iterate more slowly than an ArrayList, but it's a good choice when you need fast insertion and deletion. As of Java 5, the LinkedList class has been enhanced to implement the java.util.Queue interface. As such, it now supports the common queue methods: peek() , poll() , and offer() .

ArrayList Vs LinkedList
  1.  Both are concrete implementations of List interface. 
  2.  ArrayList provided random access where as LinkedList provide sequential access.
  3.  Both LinkedList and ArrayList maintains the insertion order.
  4. Add operation of ArrayList is slower than that of LinkedList because it may involve resizing of array which requires creating new array and copying elements to new array.
  5. Get Opration of ArrayList is much faster than LinkedList. Time complexity of get operation of ArrayList is O(1) ie. constant time wheres as that of LinkedList is O(n)
  6. Both provide fail-fast iterator and throw concurrent modification exception.
Imporant : ArrayList is preferred when the list manipulation is done less frequently and the get operation is performed more frequently whereas LinkedList is preferred when the frequency of list manipulation is more than the frequency of get operation on the list.

No comments:

Post a Comment