Difference Between ArrayList and Vector
ArrayList and Vector are both classes that implement the List interface using a resizable array. They keep insertion order, allow duplicates and null, and give fast access by index. The key difference is that Vector is synchronized and ArrayList is not. In this lesson, you will learn all the differences between them and which one to choose.
Differences Between ArrayList and Vector
| ArrayList | Vector | |
|---|---|---|
| Synchronization | Not synchronized | Synchronized: every method takes a lock |
| Thread safety | Not thread-safe | Thread-safe for single method calls |
| Performance | Faster | Slower, because of locking |
| Growth when full | Grows by 50% (10, 15, 22...) | Doubles (10, 20, 40...) |
| Initial capacity | 10, allocated on the first add | 10, allocated when created |
| Introduced in | Java 1.2 (Collections Framework) | Java 1.0 (legacy class) |
| Iteration | Iterator, ListIterator | Iterator, ListIterator and the old Enumeration |
| Legacy methods | None | addElement, elementAt, removeElement... |
| Custom growth step | No | Yes: new Vector<>(capacity, increment) |
Example: Same Operations, Different Classes
Let's see an example that runs the same code on both.
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class ListCompare {
static void fill(List<String> list) {
list.add("Navika");
list.add("Srihitha");
list.add("Advitha");
list.remove(1);
System.out.println(list.getClass().getSimpleName() + ": " + list);
}
public static void main(String[] args) {
fill(new ArrayList<>());
fill(new Vector<>());
}
}ArrayList: [Navika, Advitha]
Vector: [Navika, Advitha]Explanation: Both classes implement List, so the same method works with either and gives the same result. The difference isn't in what they do but in how they do it: each Vector call takes a lock, and each ArrayList call doesn't.
Which One to Use
- One thread: use
ArrayList. It is faster and is the standard choice. - Many threads, mostly reading: use
CopyOnWriteArrayList. - Many threads, mixed reads and writes: use
Collections.synchronizedList(new ArrayList<>())with your own locking around compound actions, or a concurrent queue. - Vector: only when maintaining old code that already uses it.
Common Mistakes
Believing Vector is the "thread-safe ArrayList" that solves every concurrency problem. Only single calls are protected.
Choosing Vector for speed. It is slower than ArrayList in single-threaded code.
Interview Corner
What is the difference between ArrayList and Vector?
Both are resizable-array implementations of List. Vector is a legacy class from Java 1.0 whose methods are all synchronized, which makes it thread-safe for single operations but slower. ArrayList, added in Java 1.2, isn't synchronized and is faster. When full, ArrayList grows by 50% while Vector doubles by default. Vector also supports the legacy Enumeration and methods such as addElement. New code should use ArrayList, or a concurrent collection when threads share the list.
Practice
- Easy: Add 100 elements to an
ArrayListand aVector, then explain why their capacities differ. - Harder: Start two threads that each add 10,000 elements to a shared
ArrayList, and print the final size. Repeat with aVector. Explain the results.
Key Takeaways
Was this tutorial helpful?