๐ŸŽ‰ Festival Deal: Save Big on Professional Courses with Real Projects โ€“ Apply Coupons FEST300OFF FEST500OFF
โ†’ Click Here โ†

Working of Vector in Java  


Steps of Working of Vector:
  • Below are the steps of working of Vector in Java:
    1. Creation of Vector:
      • A new Vector can be created using:
        Vector<String> vector = new Vector<>();
      • When a Vector is created without specifying capacity, its default capacity is 10.
      • Initially, the size of the Vector is 0 (no elements added yet).
        Size = 0, Capacity = 10
    2. Adding Elements to the Vector:
      • Elements can be added using the add() method:
        vector.add(10);
        vector.add(20);
        vector.add(30);
        vector.add(40);
      • Internally, elements are stored in a dynamic array (Object[] elementData). Each element occupies one index position.
      • Example internal structure:
        Working of Vector resizing in Java
    3. Automatic Resizing:
      • When the number of elements exceeds the current capacity, the Vector automatically increases its capacity.
      • The new capacity is calculated as:
        newCapacity = oldCapacity * 2 (i.e., it doubles its size).
      • Example:
        • Old Capacity โ†’ 10
        • New Capacity โ†’ 20
        A new internal array is created with the new capacity, and all old elements are copied into it.
      • The reference variable now points to the new array, while the old one becomes eligible for garbage collection.
    4. Accessing and Updating Elements:
      • Elements can be accessed using get(int index) and updated using set(int index, E element).
      • Since Vector uses an index-based array, both operations are very fast (O(1)).
      • Example:
        System.out.println(vector.get(1)); // prints 20
        vector.set(1, 222);
        System.out.println(vector);  // prints [10, 222, 30, 40]
    5. Thread-Safety (Synchronization):
      • All methods of Vector are synchronized, meaning only one thread can access it at a time.
      • This ensures thread-safety but may reduce performance compared to non-synchronized collections like ArrayList.
      • Example:
        Vector<Integer> v = new Vector<>();
        v.add(10);
        v.add(20);
    6. Traversal of Vector:
      • Vector can be traversed using Iterator, ListIterator, Enumeration, or an enhanced for-each loop.
      • Example:
        Enumeration<String> en = vector.elements();
        while (en.hasMoreElements())
        {
            System.out.println(en.nextElement());
        }
      • Enumeration is the legacy iterator introduced with Vector.

Vector is Good for:
  1. Thread-safe operations (Synchronization):
    • All major methods in Vector are synchronized, making it thread-safe.
    • Suitable when multiple threads access and modify the same list concurrently.
  2. Frequent read operations:
    • Provides fast access to elements using index-based retrieval (get(int index)).
    • Stores elements in a contiguous memory block like ArrayList, improving cache locality.
  3. Automatic resizing:
    • When capacity is reached, Vector automatically increases its capacity by 100% (doubles it).
    • Ensures continuous storage without manual resizing.
  4. Legacy system support:
    • Commonly used in older applications for backward compatibility with preโ€“JDK 1.2 code.
Vector is Not Good for:
  1. Poor performance in single-threaded environments:
    • Synchronization introduces unnecessary overhead when thread safety is not required.
    • ArrayList performs better in such scenarios.
  2. Frequent insertions/deletions in middle:
    • Requires shifting elements after every insertion or removal โ€” making it slower.
  3. Memory overhead due to resizing:
    • When capacity doubles, unused memory may remain unutilized until elements fill it.
  4. Obsolete compared to modern classes:
    • It is considered a legacy class and is largely replaced by ArrayList and CopyOnWriteArrayList.
Summary Table:
Operation Performance Complexity
Access by index (get) Fast O(1)
Update by index (set) Fast O(1)
Add at end Fast (amortized) O(1)
Insert/Delete in middle Slow (due to shifting) O(n)
Remove by value Slow O(n)
Search (contains) Moderate O(n)
Iteration Fast O(n)
Thread Safety Yes (synchronized) Overhead present