0
0
JavaComparisonBeginner · 3 min read

ArrayList vs Vector in Java: Key Differences and Usage

ArrayList and Vector are both resizable array implementations in Java, but Vector is synchronized making it thread-safe, while ArrayList is not synchronized and faster in single-threaded contexts. Use ArrayList for better performance when thread safety is not needed, and Vector when you require synchronized operations.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of ArrayList and Vector based on key factors.

FactorArrayListVector
SynchronizationNot synchronized (not thread-safe)Synchronized (thread-safe)
PerformanceFaster in single-threaded environmentSlower due to synchronization overhead
Growth RateIncreases by 50% when resizedDoubles size when resized
LegacyIntroduced in Java 1.2Legacy class from Java 1.0
Use CasePreferred for non-threaded applicationsUsed when thread safety is required
IteratorFail-fast iteratorFail-fast iterator
⚖️

Key Differences

ArrayList and Vector both implement the List interface and use dynamic arrays internally. The main difference is that Vector methods are synchronized, meaning only one thread can access them at a time, which makes it thread-safe but slower. ArrayList does not have this synchronization, so it performs better in single-threaded scenarios.

Another difference is how they grow when more space is needed. ArrayList increases its size by 50% each time it needs more capacity, while Vector doubles its size, which can lead to more memory usage. Also, Vector is considered a legacy class introduced before the Java Collections Framework, while ArrayList was introduced later as part of the framework.

Both use fail-fast iterators, which means if the list is modified while iterating (except through the iterator itself), they throw a ConcurrentModificationException. This behavior helps detect concurrent modification bugs.

⚖️

Code Comparison

This example shows how to add elements and iterate over an ArrayList.

java
import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        for (String fruit : list) {
            System.out.println(fruit);
        }
    }
}
Output
Apple Banana Cherry
↔️

Vector Equivalent

This example shows the same operations using a Vector.

java
import java.util.Vector;

public class VectorExample {
    public static void main(String[] args) {
        Vector<String> vector = new Vector<>();
        vector.add("Apple");
        vector.add("Banana");
        vector.add("Cherry");

        for (String fruit : vector) {
            System.out.println(fruit);
        }
    }
}
Output
Apple Banana Cherry
🎯

When to Use Which

Choose ArrayList when you do not need thread safety and want better performance in single-threaded or externally synchronized environments. It is the preferred choice for most modern Java applications.

Choose Vector only if you need a thread-safe implementation and cannot use external synchronization or other concurrent collections like CopyOnWriteArrayList. However, Vector is generally considered outdated and less efficient.

Key Takeaways

ArrayList is faster but not synchronized; Vector is synchronized but slower.
Vector is a legacy class; prefer ArrayList in new code unless thread safety is required.
Both grow dynamically but at different rates: ArrayList by 50%, Vector doubles.
Use ArrayList for most cases; use Vector only for simple thread-safe needs.
Both provide fail-fast iterators to detect concurrent modifications.