Difference between ArrayList and Vector

Share


T
his is one of the most famous questions which interviewers consider in an interview. Before we actually see the differences, let me give you a brief description of both.

ArrayList is the implementation of List Interface. The Following table summaries the characteristics of ArrayList.

ArrayList

It is not synchronized ( so, this is not thread-safe).

ArrayList class is implemented using Array internally as data structure and can be dynamically resizable. By default, ArrayList increases by half of its size when its size is increased.

Vector is the implementation of List Interface. The Following table summaries the characteristics of Vector.

Vector

It is synchronized ( so, this is thread-safe).

Vector class is implemented using Array internally as data structure and can be dynamically resizable. By default, Vector doubles the size of its array when its size is increased

ArrayList Vs VectorArrayList Vs Vector

ArrayList is not synchronized. So, all the methods of ArrayList are not thread-safe. It should not be used in a multi-threaded environment.
Vector is synchronized. So, all the methods of Vector are thread-safe. It can be used in a multi-threaded environment.

ArrayList is faster (since it is not synchronized).
Vector is slow (since it is synchronized).

ArrayList increases by half of its size when its size is increased.
Vector doubles the size of its array when its size is increased.

 Which one you should choose – Vector or ArrayList?

The only main difference which I have found between the two is – Vector is synchronized and ArrayList is not. And, since synchronization is a costly operation to perform (in terms of performance), Vector is slower than ArrayList. So, you should use Vector only when there is a need of thread-safe operations.

ArrayList can be synchronized using the Java collections framework utility class and then ArrayList itself can be used in place of Vector.

When you use Vector or ArrayList, always initialize to the largest capacity that the Java program will need. Since incrementing the size is a costlier operation.