Feature | ArrayList | Vector |
---|---|---|
Introduced in | JDK 1.2 (Collections Framework) | JDK 1.0 (legacy class) |
Thread-safe? | ❌ No (not synchronized) | ✅ Yes (synchronized) |
Performance | ✅ Faster (no locking overhead) | ❌ Slower (due to synchronization) |
Growth mechanism | Grows by 50% when full | Grows by 100% (doubles size) |
Part of Collections API? | ✅ Yes | ✅ Retroactively made part of it |
Legacy? | ❌ Modern | ✅ Legacy — replaced by ArrayList |
🔧 Thread Safety: The Key Difference
✅ Vector
is synchronized:
Every method (like add()
, get()
, remove()
) is thread-safe by default.
Vector<String> vector = new Vector<>();
vector.add("A"); // internally synchronized
Good for multi-threaded environments, but comes at a performance cost.
❌ ArrayList
is not synchronized:
Faster for single-threaded environments.
ArrayList<String> list = new ArrayList<>();
list.add("A"); // not thread-safe
You must manually synchronize it if used by multiple threads:
List<String> syncList = Collections.synchronizedList(new ArrayList<>());
🚀 Performance
- In single-threaded apps → ArrayList is faster than Vector
- In multi-threaded apps → Vector is slower, but safe
- Modern recommendation: Use
ArrayList
+ manual sync or a concurrent list (CopyOnWriteArrayList
) instead ofVector
📈 Growth Behavior
When internal capacity is full:
List Type | Grows by |
---|---|
ArrayList | 50% of current size |
Vector | 100% (doubles) |
This affects memory usage and performance during large-scale list expansions.
🧪 Example
List<String> list = new ArrayList<>();
List<String> vector = new Vector<>();
list.add("Java");
vector.add("Java");
They both support:
- Random access
- Indexed access (
get(i)
) - Duplicates
- Ordered insertion
But one is modern and fast, the other is legacy and synchronized.
🧠 Summary: Which one should you use?
Use Case | Use This |
---|---|
Single-threaded, fast access | ✅ ArrayList |
Multi-threaded (rare today) | ⚠️ Avoid Vector ; use CopyOnWriteArrayList or manual sync |
Legacy system compatibility | 🧓 Vector (only if required) |