Java.Collections.What is the difference between ArrayList and Vector?

FeatureArrayListVector
Introduced inJDK 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 mechanismGrows by 50% when fullGrows 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 of Vector

📈 Growth Behavior

When internal capacity is full:

List TypeGrows by
ArrayList50% of current size
Vector100% (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 CaseUse This
Single-threaded, fast accessArrayList
Multi-threaded (rare today)⚠️ Avoid Vector; use CopyOnWriteArrayList or manual sync
Legacy system compatibility🧓 Vector (only if required)
This entry was posted in Без рубрики. Bookmark the permalink.