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

Carvia Tech | December 25, 2017 | 1 min read | 206 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 Callable and Runnable Interface?
  2. What is difference between HashMap and HashSet
  3. Difference between HashMap, LinkedHashMap and TreeMap
  4. What is difference between ExecutorService submit and execute method
  5. What is difference between sleep() and wait() method in Java?
  6. What is difference between JDK JRE and JVM
  7. Difference between HashMap and ConcurrentHashMap


Find more on this topic:
Core Java image
Core Java

Core Java - OOP Concepts, Garbage Collection, Multi-threading, Collections Framework, Java 8 Features, Lambda Functions, Streams.

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info