Difference between ArrayList and Vector in Java

Here you will get to know the difference between ArrayList and Vector in Java programming language.

ArrayList

It is a class in Java that implements List interface. Unlike traditional array data structure, it is not fixed in length and doesn’t require its size to be specified in order to declare it. They can grow and can be reduced dynamically as per need. Apart from these benefits ArrayList class has many pre-defined methods which could help the developers save a lot of time.

Also Read: Difference between Array and ArrayList in Java

Implementation in Java

import java.util.*;

public class ArrayListEg {
   public static void main(String args[]) {
      /*Creation of ArrayList: I'm going to add String*/
	  ArrayList<String> obj = new ArrayList<String>();

	  /*This is how elements should be added to the array list*/
	  obj.add("Jack");
	  obj.add("John");
	  obj.add("Steve");
	  obj.add("Joey");

	  /* Displaying array list elements */
	  System.out.println("Currently the array list has following elements:"+obj);

	  /*Add element at the given index*/
	  obj.add(0, "Ross");
	  obj.add(1, "Monica");

	  System.out.println("Current array list is:"+obj);

	  /*Remove elements from array list like this*/
	  obj.remove("Steve");
	  
	  System.out.println("Current array list is:"+obj);

	  /*Remove element from the given index*/
	  obj.remove(1);

	  System.out.println("Current array list is:"+obj);
   }
}

Output

Currently the array list has following elements:[Jack, John, Steve, Joey]
Current array list is:[Ross, Monica, Jack, John, Steve, Joey]
Current array list is:[Ross, Monica, Jack, John, Joey]
Current array list is:[Ross, Jack, John, Joey]

Vector

Like ArrayList, Vector also implements List interface. It also supports dynamic size alteration. Vector is synchronized which means that any method which is used in Vector is thread safe i.e. only one thread can perform operations at one time on a vector. This can make it comparatively slower than ArrayList.

Implementation in Java

import java.util.*;

public class VectorExample {

   public static void main(String args[]) {
      /* Vector of initial capacity(size) of 2 */
      Vector<String> vec = new Vector<String>(2);

      /* Adding elements to a vector*/
      vec.addElement("Apple");
      vec.addElement("Orange");
      vec.addElement("Kiwi");
      vec.addElement("Mango");


      /* check size and capacityIncrement*/
      System.out.println("Size is: "+vec.size());
      System.out.println("Default capacity increment is: "+vec.capacity());

      vec.addElement("fruit1");
      vec.addElement("fruit2");
      vec.addElement("fruit3");

      /*size and capacityIncrement after two insertions*/
      System.out.println("Size after addition: "+vec.size());
      System.out.println("Capacity after increment is: "+vec.capacity());

      /*Display Vector elements*/
      Enumeration en = vec.elements();
      System.out.println("\nElements are:");
      while(en.hasMoreElements())
         System.out.print(en.nextElement() + " ");
   }
}

Output

Size is: 4
Default capacity increment is: 4
Size after addition: 7
Capacity after increment is: 8

Elements are:
Apple Orange Kiwi Mango fruit1 fruit2 fruit3

Difference between ArrayList and Vector in Java

ArrayList Vector
1. ArrayList is not thread-safe leading to being non-synchronized. So it gives better performance. Vector is thread-safe meaning its synchronized. So its performance is not good as ArrayList.
2. It uses Iterator interface to traverse elements. Vector can use both Enumeration and Iterator interface to traverse the elements.
3. If inserting an element into the ArrayList exceeds capacity then it increases its size by 50%. A Vector doubles its size if inserting an element exceeds its capacity.
4. ArrayList does not define the increment size. Vector can define the increment size using setSize() method.
5. It was introduced in java version 1.2 in Java Collections framework It was there in java since the first version of the java development kit (jdk).

Thus we learnt about difference between Vector and ArrayList in Java which are both very similar and different at the same time and allow the user to use them as per their needs.

Leave a Comment

Your email address will not be published. Required fields are marked *