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.
| Factor | ArrayList | Vector |
|---|---|---|
| Synchronization | Not synchronized (not thread-safe) | Synchronized (thread-safe) |
| Performance | Faster in single-threaded environment | Slower due to synchronization overhead |
| Growth Rate | Increases by 50% when resized | Doubles size when resized |
| Legacy | Introduced in Java 1.2 | Legacy class from Java 1.0 |
| Use Case | Preferred for non-threaded applications | Used when thread safety is required |
| Iterator | Fail-fast iterator | Fail-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.
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); } } }
Vector Equivalent
This example shows the same operations using a 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"); for (String fruit : vector) { System.out.println(fruit); } } }
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.ArrayList by 50%, Vector doubles.ArrayList for most cases; use Vector only for simple thread-safe needs.