Sunday, October 10, 2010

ArrayList V/S Vector

ArrayList and Vector are widely known classes in Java world. Most of the interviewer start their discussion to explore differences between ArrayList and Vector (Sometime interviewer also forget no of differences also has limitations :(), and Just after answering this question candidates are pulled into the world of multithreading. Oh I am not going to talk about multithreading here, I will stick to title here.
Legacy Java code has used vector enormous time. For all general indexing purposes vector classes were used. But with the evolution of Java 5, vector classes lost its ground to ArrayList and Collections framework’s utility class SynchromizedList. Now it is really mandatory to know the similarity and differences between these Vector and ArrayList.

Inheritance and Hierarchy
Both the classes implements List, RandomAccess, Cloneable and Serializable interfaces. Along with that both of them extends AbstractList class.

Structure
In general both the classes have same work. Completion of the work is also quite common between both of them but still they have major differences also.


Constructor
ArrayList has 3 constructors whereas Vector has 4 constructors as shown below:

ArrayList() Vector() Default constructor instantiate an Array, containing 10 elements.
ArrayList(int capacity) Vector(int capacity)
Constructor instantiate an Array, containing Capacity elements.
Vector(int capacity, int loadfactor)

Constructor instantiate an Array containing Capacity elements. When someone adds more element than capacity, it re-instantiate the array. The new Array will have (Loadfactor + 1)*Capacity number of elements.

ArrayList(Collection) Vector(Collection) All the elements of the collection are copied to the array.


ArrayList does have user defined load factor which will impact performance when user is not aware of no of incoming elements. Reassignment of the elements will be more and more time consuming. For ArrayList load factor is 0.5 in case of Vector it is defaulted to 1.

Note: It is good practice to use LinkedList if number of elements are more and you are not doing any search operations on the basis of index.

Multithreading
Major difference is associated with Multithreading. All the method of the vector class are synchronized. When multiple threads are trying to access specific instance of vector class, then the calls will not cause any issue. Vector is a thread safe class but not ArrayList. Hence Vector class is slow performing class whereas array list access is faster.
Note: To use iterator on vector, you might need to synchronize the code.

Patterns
Adding and deletion in both the structures take O(1) time. It may take O(N) time just when array capacity has shoot the maximum size assigned. Indexing based search on both the APIs are similar as both of them use array internally.

Serialization
Array object in the ArrayList is a transient object, so while serializing the ArrayList it is required to serialize the object individually. For this reason ArrayList implements readObject and writeObject methods. In case of vector Array object is not transient. Vector implements only writeObject method.

Iterator
ArrayList has not provided any iterator whereas in vector is quipped with its own iterator/enumeration.

These were the differences I could make out from code and different geek sites. If you are aware of any other difference let me know.