Open In App

Vector vs ArrayList in Java

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

Syntax:



ArrayList: ArrayList<T> al = new ArrayList<T>();

Vector: Vector<T> v = new Vector<T>(); 

ArrayList Vs vector 

S. No.         ArrayList Vector
1. ArrayList is not synchronized. Vector is synchronized.
2. ArrayList increments 50% of the current array size if the number of elements exceeds ts capacity. Vector increments 100% means doubles the array size if the total number of elements exceeds 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 a runnable or non-runnable state until the 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.
6 ArrayList performance is high  Vector performance is low 
7 Multiple threads is allowed  only one threads are allowed .

Significant Differences between ArrayList and Vector:



How to get Synchronized version of arraylist object:

by default arraylist is object is non-synchronized but we can get synchronized version of arraylist by using collection class Synchronized List() method.




/*package whatever //do not write package name here */
  
import java.io.*;
  
class GFG {
    public static void main (String[] args) {
        public static List SynchronizedList(list1)
          // non synchronized
          ArrayList l1 = new ArrayList();
          // Synchronized
          List l= Collections.SynchronizedList(l1);
    }
}

Note: ArrayList is preferable when there is no specific requirement to use vector.




// Java Program to illustrate use 
// of ArrayList and Vector in Java
  
import java.io.*;
import java.util.*;
  
class GFG
{
    public static void main (String[] args)
    {
        // creating an ArrayList
        ArrayList<String> al = new ArrayList<String>();
  
        // adding object to arraylist
        al.add("Practice.GeeksforGeeks.org");
        al.add("www.GeeksforGeeks.org");
        al.add("code.GeeksforGeeks.org");
        al.add("contribute.GeeksforGeeks.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.GeeksforGeeks.org
www.GeeksforGeeks.org
code.GeeksforGeeks.org
contribute.GeeksforGeeks.org

Vector elements are:
Practice
quiz
code

How to choose between ArrayList and Vector? 


Article Tags :