Collections Framework
Vector
Analyze Vector legacy synchronized structures, expansion rules, and comparison to ArrayList.
Interview: Focuses on method level synchronization, 2x expansion factor, and thread safety overhead comparison.
A Vector is a legacy thread-safe resizable-array implementation. Methods inside Vector are synchronized at the method level, which introduces lock acquisition overhead for single-threaded operations.
Synchronized Methods
Every read and write operation is protected by a monitor lock (synchronized keyword on methods), blocking concurrent access.
Capacity Doubling
Expands capacity by 100% (2x size) on resize, which is more aggressive than ArrayList's 50% growth rate.
Legacy API
Exposes older methods like addElement and elements() alongside the standard JCF List interface methods.
Vector vs ArrayList
Although Vector implements List, it should generally be avoided:
- Locking Overhead: In single-threaded applications, Vector locks the object monitor on every operation, degrading performance.
- Compound Operations: Method-level synchronization does not make compound operations (like check-then-act) thread-safe. Synchronization must still be managed externally.
Common Pitfalls
- Assuming Thread-Safety: Believing a code sequence like
if(!vector.contains(item)) { vector.add(item); }is thread-safe without external locks. A race condition exists between the two calls. - Unnecessary lock contention: Sharing a Vector across threads for read-only access, which blocks threads needlessly due to synchronized read methods.
Best Practices
- Use ArrayList: In almost all applications, use ArrayList. If thread-safety is required, use
Collections.synchronizedListor concurrent collection implementations. - Use Vector only for legacy integration: Only use Vector when integrating with third-party libraries that explicitly require it in their API signatures.
Interview-Relevant Information
Q1: What are the two main differences between Vector and ArrayList?
Answer: 1) Vector is synchronized at the method level, making it thread-safe but slower. ArrayList is unsynchronized. 2) Vector doubles its capacity (2x size) on resize, while ArrayList expands by 50% (1.5x size).
Q2: Does method synchronization make Vector's iteration thread-safe?
Answer: No. If one thread iterates over a Vector while another thread modifies it structurally, it will throw a ConcurrentModificationException. The iteration loop must be protected using external synchronization on the Vector object monitor.
Quick Checklist
Can you explain why method-level synchronization is slow, contrast growth sizes of Vector vs ArrayList, and explain why check-then-act operations require external lock protections? If yes, you understand Vector.
Use Cases
Integrating with older legacy enterprise APIs written prior to JCF introduction.
Quick single-object synchronization prototypes where thread contention is absent.
Common Mistakes
Relying on Vector method-level locks to protect complex multithreaded logic flows.
Choosing Vector for single-threaded data storage arrays.