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

  1. Dynamic Sizing: Like ArrayList, Vector resizes itself automatically as elements are added or removed.

  2. Resizable Array: The array dynamically expands as needed, doubling its size when it runs out of space.

  3. Thread-Safety: Vector is synchronized, meaning it can be safely used in multithreaded environments.

  4. Allowing Duplicates: Vector allows duplicate elements, just like ArrayList.

  5. Legacy Class: Vector was part of the original Java, whereas ArrayList was introduced later with better performance characteristics.

  6. Index-Based Access: Elements in a Vector can be accessed by index like an array.

  7. Growth Factor: By default, Vector grows by doubling its size whenever it runs out of space, but this can be customized.

  8. Element Access: It allows retrieving elements from any position using get() and modifying them with set().

  9. Thread-Safe Operations: Since it is synchronized, operations like add(), remove(), and get() are thread-safe.

  10. Bulk Operations: It provides methods for performing bulk operations such as addAll(), removeAll(), and retainAll().

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(): Returns true if 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): Returns true if 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 ?

  1. Synchronization Overhead: The synchronization in Vector can be costly in performance, especially when it’s not needed. For concurrent access, CopyOnWriteArrayList or other concurrent collections are preferred.

  2. Better Alternatives: ArrayList provides similar functionality without the overhead of synchronization and is generally faster.

  3. Legacy Class: Vector is 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 like CopyOnWriteArrayList or ArrayBlockingQueue.

  • Legacy Code: In some cases, legacy systems might still use Vector and refactoring to a more modern collection like ArrayList may not be immediately feasible.

Last updated