HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Collections Framework / Java ArrayList / Difference between ArrayList vs Vector in Java

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?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

ArrayList Methods

  • ArrayList – Introduction
  • ArrayList – add()
  • ArrayList – addAll()
  • ArrayList – clear()
  • ArrayList – clone()
  • ArrayList – contains()
  • ArrayList – ensureCapacity()
  • ArrayList – forEach()
  • ArrayList – get()
  • Arraylist – indexOf()
  • Arraylist – lastIndexOf()
  • ArrayList – listIterator()
  • ArrayList – remove()
  • ArrayList – removeAll()
  • ArrayList – removeIf()
  • ArrayList – retainAll()
  • ArrayList – sort()
  • ArrayList – spliterator()
  • ArrayList – subList()
  • ArrayList – toArray()

ArrayList Examples

  • ArrayList – Initialize arraylist
  • ArrayList – Iteration
  • ArrayList – Add/replace element
  • ArrayList – Add multiple elements
  • ArrayList – Check empty list
  • ArrayList – Remove element
  • ArrayList – Replace element
  • ArrayList – Empty arraylist
  • ArrayList – Synchronized arraylist
  • ArrayList – Compare two lists
  • ArrayList – Remove duplicates
  • ArrayList – Merge two lists
  • ArrayList – Serialization
  • ArrayList – Swap two elements
  • Convert ArrayList to Array
  • Convert Array to ArrayList
  • Convert HashSet to ArrayList
  • Convert LinkedList to ArrayList
  • Convert Vector to ArrayList
  • ArrayList vs LinkedList
  • ArrayList vs Vector

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces