Back to Study Material Java

Vector in Java

Vector is one of Java's original collection classes, present since JDK 1.0, predating the Collections Framework introduced in JDK 1.2. Like ArrayList, it u...

Vector in Java

History and Design of Vector

Vector is one of Java's original collection classes, present since JDK 1.0, predating the Collections Framework introduced in JDK 1.2. Like ArrayList, it uses a resizable array internally. The key difference is that every public method in Vector is synchronized, making it inherently thread-safe for individual method calls.

When the Collections Framework arrived, Vector was retrofitted to implement the List interface so it could participate in the new hierarchy. This allowed existing Vector-based code to interoperate with new framework utilities like Collections.sort without rewriting.

Vector's resizing strategy differs from ArrayList. By default, Vector doubles its capacity when it runs out of room (new size = 2 * old capacity), while ArrayList grows by 50%. You can also specify a custom capacityIncrement in the constructor: new Vector<>(10, 5) starts at capacity 10 and grows by 5 each time. This gives finer control but can lead to more frequent allocations if the increment is too small.

Performance Implications of Synchronization

Every method in Vector acquires an intrinsic lock on the Vector object before executing. This prevents two threads from simultaneously modifying the structure, avoiding data corruption. However, this lock acquisition has a cost even in single-threaded code, making Vector consistently slower than ArrayList when thread safety is not needed.

Furthermore, synchronization on individual methods does not make compound operations atomic. An operation like if (v.isEmpty()) v.add(x) is not thread-safe even with Vector because another thread can add an element between the isEmpty check and the add call. Proper compound operations still require external synchronization, making Vector's built-in synchronization less useful than it appears.

Time complexities mirror ArrayList: O(1) amortized add at tail, O(1) get by index, O(n) add or remove at arbitrary position, and O(n) contains.

Modern Usage and Interview Context

In modern Java, Vector is considered a legacy class. The recommended alternatives are ArrayList for single-threaded use, Collections.synchronizedList(new ArrayList<>()) for simple thread safety, and CopyOnWriteArrayList for read-heavy concurrent scenarios. Java's java.util.concurrent package provides superior concurrent data structures like ConcurrentLinkedDeque that use lock-free algorithms for better scalability under contention.

A quick example: Vector<Integer> v = new Vector<>(); v.add(10); v.add(20); System.out.println(v.capacity()); prints 10 (the default initial capacity), and after a third add beyond capacity it would print 20.

An interview tip: if asked whether you would use Vector in a new project, the honest answer is no. Explain that synchronization at the method level is too coarse-grained for most concurrent use cases and that CopyOnWriteArrayList or explicit locking around an ArrayList gives better performance and clearer concurrency semantics. This shows awareness of Java's evolution beyond its earliest APIs.