What is difference between Vector and ArrayList, which one shall be preferred

Upasana | December 25, 2017 | 1 min read | 238 views


General Guidance

One should prefer to use ArrayList over Vector because we rarely need what vector has extra to offer.

Vector is synchronized for concurrent modification. But Vector synchronizes on each individual operation. That’s almost never what one want to do. Generally you want to synchronize a whole sequence of operations.

Synchronizing individual operations is both less safe (if you iterate over a Vector, for instance, you still need to take out a lock to avoid anyone else changing the collection at the same time, which would cause a ConcurrentModificationException in the iterating thread) but also slower (why take out a lock repeatedly when once will be enough)?

There is always a overhead (CPU and Memory) of locking even when you don’t need to.

In almost all scenario’s you can utilize ArrayList in your application code, if you are looking for a synchronized version of List you can decorate a collection using the calls such as Collections.synchronizedList(List<T> list), as shown below-

List<String> persons = new ArrayList<>();
List<String> synchronizedList = Collections.synchronizedList(persons);

As for a Stack equivalent - you can have a look at Deque/ArrayDeque.

As a result, Vector is often considered deprecated nowadays.

Summary of Differences between Vector and ArrayList

  1. Vector methods is synchronized, ArrayList is not.

  2. Data Growth - Vector doubles its size when its full, ArrayList increases its size by 50% when its full.

  3. Vector class retrofitted to implement the List interface, making it a member of the Java Collections Framework in JDK 1.2


Top articles in this category:
  1. What is difference between HashMap and HashSet
  2. Difference between Callable and Runnable Interface
  3. What is difference between sleep() and wait() method in Java?
  4. Difference between ExecutorService submit and execute method
  5. Difference between HashMap and ConcurrentHashMap
  6. Difference between HashMap, LinkedHashMap and TreeMap
  7. What is purpose of Collections.unmodifiableCollection

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.