Vector vs ArrayList in Java

Spread the love

ArrayList and Vectors both implement the List interface and both use (dynamically resizable) arrays for its internal data structure, much like using an ordinary array.
Syntax:

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

Major Differences between ArrayList and Vector:

  1. Synchronization: Vector is synchronized, which means only one thread at a time can access the code, while ArrayList is not synchronized, which means multiple threads can work on ArrayList at the same time. For example, if one thread is performing an add operation, then there can be another thread performing a remove operation in a multithreading environment.
    If multiple threads access ArrayList concurrently, then we must synchronize the block of the code which modifies the list structurally or alternatively allows simple element modifications. Structural modification means addition or deletion of an element(s) from the list. Setting the value of an existing element is not a structural modification.
  2. Performance: ArrayList is faster, since it is non-synchronized, while vector operations give slower performance since they are synchronized (thread-safe). If one thread works on a vector, it has acquired a lock on it, which forces any other thread wanting to work on it to have to wait until the lock is released.
    Data Growth: ArrayList and Vector both grow and shrink dynamically to maintain optimal use of storage – but the way they resize is different. ArrayList increments 50% of the current array size if the number of elements exceeds its capacity, while vector increments 100% – essentially doubling the current array size.
    Traversal: Vector can use both Enumeration and Iterator for traversing over elements of vector while ArrayList can only use Iterator for traversing.
// Java Program to illustrate use of ArrayList 
// and Vector in Java 
import java.io.*; 
import java.util.*; 
  
class Solution 
{ 
    public static void main (String[] args) 
    { 
        // creating an ArrayList 
        ArrayList<String> al = new ArrayList<String>(); 
  
        // adding object to arraylist 
        al.add("Practice.dailycodebuffer.org"); 
        al.add("quiz.dailycodebuffer.org"); 
        al.add("code.dailycodebuffer.org"); 
        al.add("contribute.dailycodebuffer.org"); 
  
        // traversing elements using Iterator' 
        System.out.println("ArrayList elements are:"); 
        Iterator it = al.iterator(); 
        while (it.hasNext()) 
            System.out.println(it.next()); 
  
        // creating Vector 
        Vector<String> v = new Vector<String>(); 
        v.addElement("Practice"); 
        v.addElement("quiz"); 
        v.addElement("code"); 
  
        // traversing elements using Enumeration 
        System.out.println("\nVector elements are:"); 
        Enumeration e = v.elements(); 
        while (e.hasMoreElements()) 
            System.out.println(e.nextElement()); 
    } 
} 

Output:

ArrayList elements are:
Practice.dailycodebuffer.org
quiz.dailycodebuffer.org
code.dailycodebuffer.org
contribute.dailycodebuffer.org

Vector elements are:
Practice
quiz
code

How to choose between ArrayList and Vector?

  • ArrayList is unsynchronized and not thread-safe, whereas Vectors are. Only one thread can call methods on a Vector at a time, which is a slight overhead, but helpful when safety is a concern. Therefore, in a single-threaded case, ArrayList is the obvious choice, but where multithreading is concerned, vectors are often preferable.
  • If we don’t know how much data we are going to have, but know the rate at which it grows, Vector has an advantage, since we can set the increment value in vectors.
  • ArrayList is newer and faster. If we don’t have any explicit requirements for using either of them, we use ArrayList over vector.