Difference between ArrayList vs Vector in Java

ArrayList and Vector, both implements java.util.List interface and provide capability to store and get objects within using simple API methods. Still they are different in many aspects and we need to understand both classes in detail to make a wise decision when to use which class.

1. ArrayList vs Vector – Thread safety

Vector is a synchronized collection and ArrayList is not.

It simply means that when working on concurrent applications, we can use Vector without any addtional synchronization control implemented by developer using synchronized keyword. Public methods inside vector are defined synchronized which make all operations in vector safe for concurrency needs.

To use arraylist in concurrent application, we must explicitely control the thread access to instance to make application work as intended. If we want to get sunchronized version of arraylist, then we can use Collections.synchronizedList() method.

Returned list is an internal implementation of List interface different from arraylist. But it have the same capability as arraylist class.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        ArrayList<String> namesList = new ArrayList<>();
        
        namesList.add("alex");
        namesList.add("brian");
        namesList.add("charles");
        namesList.add("david");
        
        //Synchronized list
        List<String> syncNamesList = Collections.synchronizedList(namesList);
        
        syncNamesList.add("edwin");
        
        System.out.println(syncNamesList);
    }
}

Program output.

[alex, brian, charles, david, edwin]

2. ArrayList vs Vector – Capacity increment

By default when vector needs to increase capacity to add an element (when existing capacity is filled), it increases the capacity by 100%. It means vector size grows to double of previous capacity. We can overide the default capacity using constructor public Vector(int initialCapacity, int capacityIncrement). Here second argument is the amount by which the capacity is increased when the vector overflows.

In ArrayList, by default capacity grows by 50% of existing capacity. In arraylist, we can define the initial capacity but not the capacity increment factor.

3. ArrayList vs Vector – Performance

Both collections have a backing array on which they store and search elements. So essentially there is not much performance difference in add and get operations.

Real performance difference comes when we take synchronization into consideration. ArrayList is non-synchronized so there is no time lapse in thread safety. Whereas Vector is synchronized, so it has some overhead in thread management/ locking etc.

4. ArrayList vs Vector – ConcurrentModificationException

There is one difference on how these colelction handle the iteration while the collection is still modifying by program.

ArrayList provide iterators, which are fail-fast. As soon as we modify the arraylist structure (add or remove elements), the iterator will throw ConcurrentModificationException error.

Vector provide iterator as well as enumeration. Iterators are fail-fast by enumerations are not. If we modify the vector during iteration over enumeration, it does not fail.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

public class ArrayListExample {
	public static void main(String[] args) 
	{
		//Vector
		Vector<String> vector = new Vector<>(Arrays.asList("A","B","C"));
		
		Enumeration<String> vectorEnums = vector.elements();
		
		while(vectorEnums.hasMoreElements()) {
			String value = vectorEnums.nextElement();
			if("B".equals(value)) {
				vector.add("D");
			}
			System.out.println(value);
		}
		
		System.out.println("================");
				
		//ArrayList
		ArrayList<String> list = new ArrayList<>(Arrays.asList("A","B","C"));
		
		Iterator<String> listItr = list.iterator();
		
		while(listItr.hasNext()) {
			String value = listItr.next();
			if("B".equals(value)) {
				list.add("D");
			}
			System.out.println(value);
		}
	}
}

Program output.

A
B
C
D
================
A
B
Exception in thread "main" 
java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
	at java.util.ArrayList$Itr.next(ArrayList.java:851)
	at com.howtodoinjava.ArrayListExample.main(ArrayListExample.java:33)

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs
Vector Java Docs

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.