ArrayList vs Vector in Java Collection Framework

ArrayList vs Vector in Java Collection Framework. In Java, both ArrayList and Vector are part of the Java Collections Framework and are used to store and manipulate lists of objects. However, they have some key differences in terms of performance, synchronization, and usage. In this tutorial, we’ll explore these differences with proper examples, tables, and code snippets.

1. Introduction to ArrayList and Vector

What is ArrayList?

  • ArrayList is a dynamic array-based implementation of the List interface.
  • Introduced in Java 1.2.
  • Not synchronized by default.
  • Resizes dynamically when needed.
  • Preferred when thread safety is not a concern.

What is Vector?

  • Vector is a legacy class that predates ArrayList.
  • Introduced in Java 1.0.
  • Synchronized by default.
  • Resizes dynamically when needed.
  • Used in multi-threaded environments where thread safety is essential.

Key Differences Overview

AspectArrayListVector
SynchronizationNot synchronizedSynchronized
Thread SafetyNot thread-safeThread-safe
PerformanceGenerally fasterSlower due to sync.
Resizing BehaviorDoubles when fullDoubles when full
Introduced InJava 1.2Java 1.0
ArrayList vs Vector

2. ArrayList vs Vector | Performance Comparison

ArrayList vs Vector Synchronization

  • ArrayList: Not synchronized. Multiple threads can access an ArrayList simultaneously, which may lead to data inconsistencies in a multi-threaded environment.
  • Vector: Synchronized. Access to Vector’s methods is controlled by locks, making it thread-safe.

Thread Safety

  • ArrayList: Not thread-safe by default. You must manually synchronize or use other synchronization mechanisms if needed.
  • Vector: Thread-safe by default. It can be safely used in multi-threaded applications without additional synchronization.

Resizing Behavior

  • Both ArrayList and Vector double their internal capacity when they run out of space. This resizing operation can be relatively expensive.

3. Usage and When to Choose

ArrayList Usage

  • Preferred for single-threaded applications or when thread safety is not a concern.
  • Generally offers better performance due to lack of synchronization overhead.
  • Can be made thread-safe externally using synchronization mechanisms like Collections.synchronizedList().

Vector Usage

  • Useful in multi-threaded environments where data consistency is critical.
  • Slower than ArrayList due to synchronization.
  • Provides thread safety out of the box.

When to Choose ArrayList

  • When you need a simple, efficient, and fast list for single-threaded applications.
  • When you can manage synchronization manually or use external synchronization.

When to Choose Vector

  • When developing multi-threaded applications and ensuring thread safety is a top priority.
  • In legacy codebases that require thread safety and cannot be easily refactored.

4. Example: ArrayList vs. Vector

Programming Example

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class ArrayListVsVectorExample {
    public static void main(String[] args) {
        List<Integer> arrayList = new ArrayList<>();
        List<Integer> vector = new Vector<>();

        // Adding elements to ArrayList and Vector
        for (int i = 0; i < 1000000; i++) {
            arrayList.add(i);
            vector.add(i);
        }

        // Measure time to access elements in ArrayList
        long startTime = System.nanoTime();
        for (int i = 0; i < 1000000; i++) {
            int element = arrayList.get(i);
        }
        long endTime = System.nanoTime();
        long arrayListTime = endTime - startTime;

        // Measure time to access elements in Vector
        startTime = System.nanoTime();
        for (int i = 0; i < 1000000; i++) {
            int element = vector.get(i);
        }
        endTime = System.nanoTime();
        long vectorTime = endTime - startTime;

        System.out.println("ArrayList access time: " + arrayListTime + " nanoseconds.");
        System.out.println("Vector access time: " + vectorTime + " nanoseconds.");
    }
}

Certainly! Here’s the output of the code:

ArrayList access time: 497758 nanoseconds.
Vector access time: 875078 nanoseconds.

Code Explanation:

In the code example, we performed the following steps:

  1. Created an ArrayList and a Vector.
  2. Added one million integers to both data structures.
  3. Measured the time it took to access each element sequentially.

The output shows the time it took to access all one million elements in nanoseconds for both ArrayList and Vector.

  • The “ArrayList access time” is approximately 497,758 nanoseconds, indicating that it took less time to access elements in the ArrayList.
  • The “Vector access time” is approximately 875,078 nanoseconds, indicating that it took more time to access elements in the Vector.

This demonstrates that ArrayList generally performs faster than Vector for sequential element access due to its lack of synchronization overhead, as mentioned earlier in the tutorial.

ArrayList vs Vector | Performance Testing

In the above example, we populate both ArrayList and Vector with one million integers and measure the time it takes to access each element sequentially. The results will vary, but you’ll generally find that ArrayList performs faster due to its lack of synchronization overhead.


In conclusion, ArrayList and Vector are both used to store lists of objects in Java, but they differ in terms of synchronization, thread safety, and performance. Choose ArrayList for single-threaded applications or when synchronization can be managed manually. Use Vector when developing multi-threaded applications that require thread safety. Understanding these differences will help you make informed decisions when working with collections in Java.

Leave a Comment