collections

Vector vs ArrayList

šŸ”¹ Vector vs ArrayList (Deep Dive, Interview-Ready)

Both Vector and ArrayList are resizable array implementations of the List interface, but they differ significantly in thread-safety, performance, and legacy design philosophy.


šŸ“Œ 1. Core Difference (One-Liner)

Vector is synchronized (thread-safe by default), while ArrayList is not, making ArrayList faster in single-threaded scenarios.


šŸ“Š 2. Detailed Comparison

FeatureVectorArrayList
SynchronizationFully synchronized (every method)Not synchronized
Thread SafetyThread-safe by defaultNot thread-safe
PerformanceSlower (locking overhead)Faster
Growth StrategyDoubles size (or uses capacityIncrement)Grows by ~50% (1.5x)
Legacy StatusLegacy class (pre-Collections)Modern (Java Collections Framework)
Iterator TypeEnumeration + IteratorIterator + ListIterator
Fail-Fast BehaviorEnumeration is NOT fail-fastIterators are fail-fast
Usage TodayRareVery common

šŸ“Œ 3. Internal Working Differences

šŸ”ø ArrayList

Internal Storage:

transient Object[] elementData;

Growth Strategy:

int newCapacity = oldCapacity + (oldCapacity >> 1); // 1.5x growth

šŸ‘‰ Optimized for:

  • Speed (No synchronization overhead)
  • Memory efficiency (Grows conservatively by 50%)

šŸ”ø Vector

Internal Storage:

protected Object[] elementData;

Growth Strategy:

int newCapacity = oldCapacity * 2; // Default 2x growth // OR: int newCapacity = oldCapacity + capacityIncrement; // If capacityIncrement is specified

šŸ‘‰ Impact: More aggressive resizing leads to potentially higher unused memory capacity.


šŸ“Œ 4. Synchronization Details (Very Important)

šŸ”ø Vector

Every method in Vector is synchronized:

public synchronized boolean add(E e) { // ... }

šŸ‘‰ The Problem: Locking is applied at the method level (on the Vector object itself). This means even simple concurrent read operations require locking, which leads to thread contention and reduced scalability in highly concurrent environments.


šŸ”ø ArrayList

  • Contains no synchronization.
  • Multiple threads modifying it concurrently will lead to a ConcurrentModificationException or inconsistent state.

āœ… How to make ArrayList thread-safe?

Use the Collections utility class:

List<String> list = Collections.synchronizedList(new ArrayList<>());

OR better yet, for high-read concurrency:

List<String> list = new CopyOnWriteArrayList<>();

šŸ“Œ 5. Performance Insight

ScenarioBetter Choice
Single-threadedArrayList
Multi-threaded (high concurrency)CopyOnWriteArrayList or ConcurrentHashMap
Multi-threaded (low write, high read)CopyOnWriteArrayList
Legacy systemsVector

šŸ‘‰ Vector is almost never preferred in modern Java applications.


šŸ“Œ 6. Fail-Fast vs Non Fail-Fast

šŸ”ø ArrayList Iterator

  • Fail-fast by default.
  • Throws ConcurrentModificationException if the list is structurally modified at any time after the iterator is created.

šŸ”ø Vector Enumeration

  • Not fail-fast.
  • Can lead to inconsistent behavior if the collection is modified during traversal, risking unpredictable results rather than failing cleanly.

šŸ“Œ 7. When Should You Use Vector?

šŸ‘‰ Practically:

  • Only when working with legacy codebases or APIs that explicitly require a Vector object.

Otherwise, prefer:

  • ArrayList (default choice)
  • Collections.synchronizedList (for thread-safe list operations)
  • CopyOnWriteArrayList (for thread-safe lists with frequent reads and rare writes)

šŸ“Œ 8. Memory & Scalability Trade-Off

AspectVectorArrayList
Memory UsageHigher (100% / 2x growth)Lower (50% / 1.5x growth)
ScalabilityPoor (heavy locking mechanism)High (when used properly)
CPU OverheadHigh (due to intrinsic locks)Low

šŸ”„ Interview Gold Statement

"Vector is a legacy, synchronized dynamic array where every operation is thread-safe but incurs significant performance overhead. ArrayList is the modern, unsynchronized alternative that is optimized for speed and memory. In modern applications, ArrayList is used by default, and external synchronization or CopyOnWriteArrayList is preferred for concurrent environments."


⚔ Final Verdict

  • āœ… Use ArrayList in 99% of cases.
  • āŒ Avoid Vector unless dealing with legacy systems.
Vector vs ArrayList | DevExCode