The Wayback Machine - https://web.archive.org/web/20250304073740/https://www.geeksforgeeks.org/vector-vs-arraylist-java/
Open In App

Vector vs ArrayList in Java

Last Updated : 16 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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:

  • 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 allow simple element modifications. Structural modification means the addition or deletion of element(s) from the list. Setting the value of an existing element is not a structural modification.

ArrayList vs Vector Java

  • 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 vector elements, while ArrayList can only use Iterator for traversing.
  • Applications: Most of the time, programmers prefer ArrayList over Vector because ArrayList can be synchronized explicitly using Collections.synchronizedList.

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.

Java




/*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




// 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("write.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
write.geeksforgeeks.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, which is slightly 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 will 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.

Kickstart your Java journey with our online course on Java Programming, covering everything from basics to advanced concepts. Complete real-world coding challenges and gain hands-on experience. Join the Three 90 Challenge—finish 90% in 90 days for a 90% refund. Start mastering Java today!


Next Article

Similar Reads

How to clone an ArrayList to another ArrayList in Java?
The clone() method of the ArrayList class is used to clone an ArrayList to another ArrayList in Java as it returns a shallow copy of its caller ArrayList. Syntax: public Object clone(); Return Value: This function returns a copy of the instance of Object. Below program illustrate the Java.util.ArrayList.clone() method: Example: Java // Java progra
2 min read
Copy Elements of One ArrayList to Another ArrayList in Java
It is the implementation class of List Interface. It allows duplicated objects/elements and as well as maintains the insertion order. You can get the element present inside the ArrayList by the index of it now you need to pass it into the getting (index) method. You can add the elements into ArrayList using the add() method. Syntax: ArrayList Initi
7 min read
What is the difference between ArrayList.clear() and ArrayList.removeAll()?
In Java, ArrayList.clear() and ArrayList.removeAll() are the two methods that are used to remove elements from an ArrayList, but they are used for different purposes. In this article will explain the difference between the two methods.1. ArrayList.clear() MethodThe clear() method of ArrayList in Java is used to remove all the elements from a list.S
3 min read
Convert Vector to ArrayList in Java
There are multiple ways to convert vector to ArrayList, using passing the Vector in ArrayList constructor and by using simple vector traversal and adding values to ArrayList. Approach 1: Create a Vector.Add some values in Vector.Create a new ArrayList.Traverse vector from the left side to the right side.Start adding each element in ArrayList.Below
3 min read
Convert ArrayList to Vector in Java
There are several ways to convert ArrayList to Vector. We can use a vector constructor for converting ArrayList to vector. We can read ArrayList elements one by one and add them in vector. Approach 1: (Using Vector Constructor) Create an ArrayList.Add elements in ArrayList.Create a vector and pass the ArrayList in Vector Constructor.Vector(Collecti
3 min read
Copy Elements of Vector to Java ArrayList
Since Vector class and ArrayList class both are part of Java Collections, ie Collection framework, so both of these classes can use methods available to the Collection framework. Copy() method is one of the methods of Collection Interface which is used to copy one list to another list, here list can be any collection like ArrayList, LinkedList, and
3 min read
Java Program to Copy Elements of ArrayList to Vector
Vector implements List Interface, like ArrayList it also maintains insertion order but it is rarely used in the non-thread environment as it is synchronized, and due to which it gives a poor performance in adding, searching, deleting, and updating of its elements. To copy elements from one collection to others, pass the object of ArrayList to the c
4 min read
Difference between ArrayList, LinkedList and Vector
ArrayList:Array List is an implemented class of List interface which is present in package java.util. Array List is created on the basis of the growable or resizable array. And Array List is an index-based data structure. In ArrayList, the element is stored in a contiguous location. It can store different data types. And random access is allowed.
11 min read
Copy Elements of One Java Vector to Another Vector in Java
Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector<Integer> gfg=new Vector<>(); Ways To copy elements o
3 min read
Java Program to Empty an ArrayList in Java
ArrayList class is a resizable array, present in 'java.util package'. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elements can be added/appended or removed from an Arra
3 min read