Difference between Vector and Arraylist in Java

In Java ArrayList and Vector classes both are implements List Interface, and both are using resizable and growable Array for internal data structure.

Syntax:-
        ArrayList: ArrayList<T> arrayList = new ArrayList<T>();
        Vector:    Vector<T> vector = new Vector<T>();

Differences with respect to :

Methods:

  • Every method present inside ArrayList is non-synchronized.
  • Every method present inside Vector is synchronized.

Thread Safety:

  • ArrayList is Non Synchronized by default, at a time multiple threads are allow to operate on ArrayList simultaneously and hence ArrayList object is Not Thread Safe, we can get Synchronized version of ArrayList using synchronizedList(List l) method of Collection class.
  • Vector is Synchronized, at a time only one thread allow to operate on Vector object and hence Vector is always Thread Safe.

Performance:

  • Performance of ArrayList is high because threads are not required to wait, at a time multiple threads can access ArrayList object.
  • Performance of Vector is low because threads are required to wait, at a time only one thread can access Vector object.

Legacy:

  • ArrayList class introduced in JDK 1.2 version and it is Not Legacy.
  • Vector class introduced in 1.0 version and it is Legacy.

Increment Size:

  • ArrayList increments 50% of current array size if the number of elements exceeds from its capacity.
  • Vector increments 100% (double) the array size if the total number of elements exceeds than its capacity.

Leave a Reply

Your email address will not be published. Required fields are marked *

*