Vector
About
Vector is a dynamic array class that implements the List interface in Java. It is part of the java.util package and is similar to ArrayList, but with a few key differences. Vector was introduced in the original version of Java and is considered legacy since newer classes like ArrayList are more commonly used due to performance reasons. However, Vector still provides a thread-safe, growable array implementation.
Features
Dynamic Sizing: Like
ArrayList,Vectorresizes itself automatically as elements are added or removed.Resizable Array: The array dynamically expands as needed, doubling its size when it runs out of space.
Thread-Safety:
Vectoris synchronized, meaning it can be safely used in multithreaded environments.Allowing Duplicates:
Vectorallows duplicate elements, just likeArrayList.Legacy Class:
Vectorwas part of the original Java, whereasArrayListwas introduced later with better performance characteristics.Index-Based Access: Elements in a
Vectorcan be accessed by index like an array.Growth Factor: By default,
Vectorgrows by doubling its size whenever it runs out of space, but this can be customized.Element Access: It allows retrieving elements from any position using
get()and modifying them withset().Thread-Safe Operations: Since it is synchronized, operations like
add(),remove(), andget()are thread-safe.Bulk Operations: It provides methods for performing bulk operations such as
addAll(),removeAll(), andretainAll().
Key Methods
add(E e): Adds an element to the end of the vector.remove(Object o): Removes the first occurrence of the specified element.get(int index): Returns the element at the specified index.set(int index, E element): Replaces the element at the specified index with a new element.size(): Returns the number of elements in the vector.clear(): Removes all elements from the vector.isEmpty(): Returnstrueif the vector is empty.capacity(): Returns the current capacity (size of the underlying array) of the vector.ensureCapacity(int minCapacity): Ensures the vector can hold at least the specified number of elements without resizing.trimToSize(): Reduces the capacity of the vector to the current size.removeAllElements(): Removes all elements from the vector.clone(): Creates a shallow copy of the vector.contains(Object o): Returnstrueif the vector contains the specified element.
Big(O) for Operations on Vector
Operation
Time Complexity
Explanation
Access by Index (get() or set())
O(1)
Direct access to an element by index, as Vector uses a dynamic array internally.
Insert at End (add(element))
O(1)(amortized)
Adding at the end is efficient unless the array needs to be resized, in which case it’s O(n).
Insert at Specific Index (add(index, element))
O(n)
Requires shifting all elements after the insertion point.
Remove by Index (remove(index))
O(n)
Elements after the removed index must be shifted.
Remove by Value (remove(element))
O(n)
Requires scanning the entire Vector to find the element, and then shifting elements.
Search by Value (indexOf() or contains())
O(n)
Requires a linear scan of the Vector to find the element.
Iteration (e.g., using for-each loop)
O(n)
Traverses all elements in the Vector.
Resize (when capacity is exceeded)
O(n)
Involves allocating a new array, copying old elements, and adding the new element.
Examples
Basic Operation
Using Generics with Vector
We can create a Vector for any type of data, ensuring type safety.
Custom Objects in Vector
Like ArrayList, We can store custom objects in Vector.
Thread-Safety in Vector (Synchronized)
Since Vector is synchronized, it can be used safely in multithreaded environments, but newer alternatives like CopyOnWriteArrayList are preferred due to better performance.
Why Vector is Not Preferred in Modern Java ?
Synchronization Overhead: The synchronization in
Vectorcan be costly in performance, especially when it’s not needed. For concurrent access,CopyOnWriteArrayListor other concurrent collections are preferred.Better Alternatives:
ArrayListprovides similar functionality without the overhead of synchronization and is generally faster.Legacy Class:
Vectoris part of the original Java versions, and while still supported, it has been superseded by newer, more efficient collections.
When to Use Vector ?
Thread-Safe Operations: If you need automatic synchronization, you can use
Vector, but better alternatives exist likeCopyOnWriteArrayListorArrayBlockingQueue.Legacy Code: In some cases, legacy systems might still use
Vectorand refactoring to a more modern collection likeArrayListmay not be immediately feasible.
Last updated