ArrayList vs Vector in java

Here we will see the difference between ArrayList and Vector in java.

 ArrayListVector
1.ArrayList is not synchronized.Vector is synchronized.
2.Since ArrayList is not synchronized,so its not suitable for use in multithreaded environment.Vector is synchronized thread safe and allow to use in multithreaded environment.
3.ArrayList introduced in java 1.2 version ,it is not a legacy classes.Vector introduced in java 1.0 version is a legacy class.
4.ArrayLIst has three direct known subclass AttributeList,RoleList and RoleUnresolvedList.Vector has direct known subclass stack.
5.we can Iterate the elements of ArrayList only through Iterator.we can iterate the elements of vector through Iterator as well as enumeration
6.The capacity of ArrayList increase by 50 % when it crossed initial capacity.he capacity of vector increased 100 % when it crossed initial capacity.
7.Since ArraList is not synchronized its faster than vector.Vector is slow in Performance.

 

Example of ArrayList and Vector – 

Example of ArrayList –

import java.util.ArrayList;
import java.util.List;
 
public class ArrayListToArrays {
	public static void main(String[] args) {
		List<String> listObject = new ArrayList<>();
 
		listObject.add("ram");
		listObject.add("mohan");
		listObject.add("shyam");
		listObject.add("mohan");
		listObject.add("ram");
 
		// we have toArray() defined in List interface which returns Object
		// array
		Object[] arraysObject = listObject.toArray();
 
		for (Object obj : arraysObject) {
			System.out.println(obj);
		}
	}
}

Output is –

ram
mohan
shyam
mohan
ram

Example of Vector –

import java.util.Iterator;
import java.util.List;
import java.util.Vector;
 
public class VectorExample {
	public static void main(String[] args) {
		List<String> list = new Vector<>();
		list.add("ram");
		list.add("mohan");
		list.add("sohan");
		list.add("shyam");
		list.add("ram");
 
		Iterator<String> it = list.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
}

Output is –

ram
mohan
sohan
shyam
ram

ArrayList is not synchronized whereas Vector is synchronized –


All methods inside Vector is synchronized. For example, add() method is defined inside Vector something like below.

  public synchronized boolean add(E e) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = e;
        return true;
    }

None of the methods of ArrayList is synchronized.

ArrayList introduced in Java 1.2 version, it is not a legacy classes whereas Vector introduced in Java 1.0 version is a legacy class-

Here we can see the documentation  ArrayList has been introduced in jdk 1.2 version whereas Vector class is there since jdk 1.0.


Performance in case of ArrayList and Vector –  Let’s see an example where we will see how ArrayList is faster than Vector.

package rrrr;
import java.util.*;
public class ArrayListVsVectorPerf {
public static void main(String[] args) {
	
	// define ArrayList Object
	List<String> listOfArrayList = new ArrayList<>();
	
	//get current time in milliseconds
	long l1 = System.currentTimeMillis();
	for(int i=0; i<1000000; i++) {
		//add ram 1M times
		listOfArrayList.add("ram");
	}
	//get current time in milliseconds
	long l2 = System.currentTimeMillis();
	
	
	//l2 - l1 will return time took to add 1M times ram in listOfArrayList
	System.out.println("time taken by ArrayList "+(l2-l1));
	
	long l3 = System.currentTimeMillis();
	List<String> listOfVector = new Vector<>();
	for(int i=0; i<1000000; i++) {
		listOfVector.add("ram");
	}
	
	
	long l4 = System.currentTimeMillis();
	
	//l2 - l1 will return time took to add 1M times ram in listOfVector
	System.out.println("time taken by Vector    "+(l4-l3));
	
	
}
}

Output –

time taken by ArrayList 18
time taken by Vector 39

 

In the above program, we try to illustrate how vector is slower than ArrayList. The output may be change but Vector is talking almost double time to add the same number of elements.