In this post, we will compare and contrast ArrayList and Vector in Java, and explain their advantages and disadvantages.

1. Overview of ArrayList

ArrayList is a class that implements the List interface and uses a dynamically resizable array as its underlying data structure to store elements. ArrayList is one of the most commonly used classes in Java, and it offers many benefits over other types of lists. Here is an example of how to create and use an ArrayList in Java:

Download  Run Code

2. Overview of Vector

Vector is a class that implements the List interface and uses a dynamically resizable array as its underlying data structure to store elements. Vector is one of the oldest classes in Java and has been available since Java 1.0. Here is an example of how to create and use a Vector in Java:

Download  Run Code

2. Similarities between ArrayList and Vector

Both ArrayList and Vector are resizable-array implementations of the List interface. ArrayList is roughly equivalent to Vector and have many similarities:

  1. Both classes are members of the Java Collections Framework and implements the List interface.
  2. Both classes internally use an array data structure to store the list.
  3. Both classes can automatically grow or shrink to accommodate new items and remove existing elements.
  4. Both classes maintain the insertion order of elements, i.e., they are ordered.
  5. The iterators returned by iterator() and listIterator() methods of ArrayList and Vector are fail-fast.
  6. Both classes permits null values and duplicates.

3. Differences between ArrayList and Vector

Now let’s discuss some of the major differences between the ArrayList and Vector implementations.

1. Synchronization

The primary difference between an ArrayList and Vector is that a Vector implementation is synchronized while an ArrayList implementation is not synchronized. This means that only a single thread can operate on a Vector method at a time, while multiple threads can operate on an ArrayList concurrently.

The ArrayList is not suitable for multithreading environments where thread safety and consistency are required. If multiple threads access ArrayList concurrently, it may lead to data corruption, inconsistency, or unexpected behavior. To make an ArrayList thread-safe, it can be synchronized externally using the Collections.synchronizedList() method.

A Vector is synchronized by default, but is not exactly thread-safe. This is because Vector synchronizes on each operation and not the whole Vector instance itself.

2. Performance

Vectors are very slow as they are synchronized, and a single thread can obtain a lock on an operation, making other threads wait until that lock is released. ArrayList, on the other hand, is much faster than a Vector as it is not synchronized and multiple threads can operate on it at the same time. This makes it fast and efficient, as it does not incur any overhead or performance penalty due to locking or blocking.

3. Storage management

Both ArrayList and Vector can grow and shrink dynamically to accommodate new elements if needed. Instead of incremental reallocation, the storage increases in chunks. Normally when new elements are added and the capacity is full, an ArrayList increases its size by half of its current size, and a Vector doubles its size. All this is taken care of automatically by the Java Virtual Machine (JVM). However, this does not affect the logical order of the elements in the list, which is determined by their index.

4. Fail-fast

For traversing the list, an ArrayList uses an iterator while a Vector uses both enumeration and iterator. The Enumeration returned by the vector method is not fail-fast. In contrast, the iterators returned by the iterator() and listIterator() methods of both Vector and ArrayList are fail-fast and throws ConcurrentModificationException if the collection is structurally modified after the iterator is created except through the iterator’s own remove() or add() methods.

4. Which implementation to use?

An ArrayList should always be preferred over a Vector. A Vector is a legacy class which deprecated and should be avoided at all costs. The Vector class was not included as part of the Java Collection Framework and was included later. We have already mentioned that a Vector is synchronized but not completely thread-safe. Vector also has the overhead of locking whether synchronization is required or not.

If a thread-safe implementation of List interface is required, we can either use CopyOnWriteArrayList class, which is a thread-safe variant of the ArrayList or synchronize ArrayList externally using the Collections.synchronizedList() method.

That’s all about the differences between ArrayList and Vector in Java.