In Java, both ArrayList and Vector are classes in the java.util
package that implement the List
interface. They are used to store dynamically sized collections of objects. While they have similarities, they also have distinct differences in terms of performance, synchronization, and usage.
This article explores the differences between ArrayList and Vector, their features, and when to use each.
Overview of ArrayList
- Introduced in: Java 1.2 (part of the Java Collections Framework).
- Definition: An ArrayList is a resizable array that allows elements to be added or removed dynamically. It is not synchronized by default, making it faster for single-threaded operations.
Key Features of ArrayList:
- Non-synchronized (not thread-safe).
- Provides better performance in single-threaded environments.
- Increases its size by 50% when the array is full.
Overview of Vector
- Introduced in: Java 1.0 (prior to the Java Collections Framework).
- Definition: A Vector is also a resizable array, but it is synchronized by default, meaning it is thread-safe and can be used in multi-threaded environments.
Key Features of Vector:
- Synchronized (thread-safe).
- Slower performance due to synchronization overhead.
- Increases its size by 100% (doubles) when the array is full.
Differences Between ArrayList and Vector
Aspect | ArrayList | Vector |
---|---|---|
Synchronization | Not synchronized, hence not thread-safe. | Synchronized, making it thread-safe. |
Performance | Faster due to lack of synchronization overhead. | Slower due to synchronization overhead. |
Growth Rate | Increases size by 50% when full. | Doubles its size (100%) when full. |
Legacy | Part of the Java Collections Framework. | Legacy class, included before Java Collections. |
Thread Safety | Requires manual synchronization for safe use in multi-threaded environments. | Thread-safe by default. |
Usage | Preferred for single-threaded applications. | Suitable for multi-threaded applications. |
Enumeration | Does not support Enumeration . |
Supports Enumeration (legacy iterator). |
Iterators | Uses Iterator for traversing elements (fail-fast). |
Uses both Iterator and Enumeration (not fail-fast). |
Key Methods in ArrayList and Vector
Common Methods:
Both ArrayList and Vector support common methods like:
add(Object o)
: Adds an element to the list.remove(Object o)
: Removes a specific element.get(int index)
: Retrieves an element at a specified index.size()
: Returns the number of elements in the list.
Additional Methods in Vector:
addElement(Object o)
: Adds an element at the end of the vector (legacy method).capacity()
: Returns the current capacity of the vector.elements()
: Returns anEnumeration
of the elements in the vector.
When to Use ArrayList or Vector
Use ArrayList When:
- You are working in a single-threaded environment.
- Performance is a priority, and thread safety is not required.
- You need a modern, efficient implementation of a dynamic array.
Use Vector When:
- You are working in a multi-threaded environment.
- You require built-in synchronization for thread safety.
- You are working with legacy code that already uses Vectors.
Example of ArrayList
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”);
System.out.println(“ArrayList: “ + list);
}
}
Example of Vector
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”);
System.out.println(“Vector: “ + vector);
}
}
While both ArrayList and Vector serve similar purposes, their differences in synchronization, performance, and use cases make them suitable for different scenarios. ArrayList is the preferred choice for most modern applications due to its efficiency and simplicity in single-threaded environments, whereas Vector is more appropriate when thread safety is required without external synchronization. Understanding these differences will help you choose the right collection class for your Java applications.