Difference Between ArrayList and Vector in Java

JavaServer Side ProgrammingProgramming

In this post, we will understand the difference between ArrayList and Vector in Java.

ArrayList

  • It is not synchronized.

  • If the number of elements exceeds the capacity of the ArrayList, it increments the current array size by 50 percent.

  • It is not thread-safe.

  • It was introduced in JDK 1.2.

  • it can only use iterator to traverse.

  • Since it is non-synchronized, it is quick.

  • It uses the Iterator interface to traverse through the elements.

Example

ArrayList<T> al = new ArrayList<T>();

Vector

  • It is synchronized.

  • It is thread safe.

  • It is a legacy class.

  • It is slow, since it is synchronized.

  • If the number of elements exceeds the capacity of the Vector, it increments the current array size by 100 percent.

  • It can use enumerator and iterator to traverse.

  • It is preferred over ArrayList.

  • It provides a multi-threading environment.

  • It holds other threads in the runnable or non-runnable state, until the current thread releases the lock on the specific object.

  • It can use either the ‘Iterator’ interface or the Enumeration interface to traverse through the elements.

Example

Vector<T> v = new Vector<T>();
raja
Published on 24-Mar-2021 12:44:31
Advertisements