ArrayList and Vector both implements List interface and maintains insertion order.

However, there are many differences between ArrayList and Vector classes that are given below.

ArrayList

Vector

1) ArrayList is not synchronized.

Vector is synchronized.

2) ArrayList increments 50% of current array size if the number of elements exceeds from its capacity.

Vector increments 100% means doubles the array size if the total number of elements exceeds than its capacity.

3) ArrayList is not a legacy class. It is introduced in JDK 1.2.

Vector is a legacy class.

4) ArrayList is fast because it is non-synchronized.

Vector is slow because it is synchronized, i.e., in a multithreading environment, it holds the other threads in runnable or non-runnable state until current thread releases the lock of the object.

5) ArrayList uses the Iterator interface to traverse the elements.

A Vector can use the Iterator interface or Enumeration interface to traverse the elements.

 ArrayList vs Vector

Example of Java ArrayList

Let's see a simple example where we are using ArrayList to store and traverse the elements.

  1. import java.util.*;    
  2. class TestArrayList21{    
  3.  public static void main(String args[]){    
  4.      
  5.   List<String> al=new ArrayList<String>();//creating arraylist    
  6.   al.add("Sonoo");//adding object in arraylist    
  7.   al.add("Michael");    
  8.   al.add("James");    
  9.   al.add("Andy");    
  10.   //traversing elements using Iterator  
  11.   Iterator itr=al.iterator();  
  12.   while(itr.hasNext()){  
  13.    System.out.println(itr.next());  
  14.   }    
  15.  }    
  16. }    
Test it Now

Output:

Sonoo
Michael
James
Andy

Example of Java Vector

Let's see a simple example of a Java Vector class that uses the Enumeration interface.

  1. import java.util.*;      
  2. class TestVector1{      
  3.  public static void main(String args[]){      
  4.   Vector<String> v=new Vector<String>();//creating vector  
  5.   v.add("umesh");//method of Collection  
  6.   v.addElement("irfan");//method of Vector  
  7.   v.addElement("kumar");  
  8.   //traversing elements using Enumeration  
  9.   Enumeration e=v.elements();  
  10.   while(e.hasMoreElements()){  
  11.    System.out.println(e.nextElement());  
  12.   }  
  13.  }      
  14. }      
Test it Now

Output:

umesh
irfan
kumar