Java Programming Handbook

    Java Vector

    The Vector class in Java is a part of the Java Collection Framework (JCF) and implements the List interface. It uses a dynamic array to store the elements and is synchronized, making it thread-safe.

    This blog provides a detailed understanding of the Vector class, its characteristics, constructors, commonly used methods, and usage examples.

    Characteristics of Vector#

    • Thread-safe: All methods are synchronized.
    • Dynamic array: Grows as elements are added.
    • Allows duplicates: Like any List, Vector allows duplicate elements.
    • Maintains insertion order.

    Declaration#

    Vector<Type> vector = new Vector<>();

    Constructors#

    ConstructorDescription
    Vector()Creates an empty vector with initial capacity of 10.
    Vector(int initialCapacity)Creates a vector with the specified initial capacity.
    Vector(int initialCapacity, int capacityIncrement)Creates a vector with specified capacity and increment.
    Vector(Collection<? extends E> c)Creates a vector containing elements of the specified collection.

    Commonly Used Methods#

    MethodDescription
    add(E e)Adds an element to the end of the vector.
    add(int index, E element)Adds an element at a specific index.
    get(int index)Returns element at specified position.
    set(int index, E element)Replaces element at specified position.
    remove(int index)Removes element at specified index.
    size()Returns the number of elements in the vector.
    isEmpty()Checks if vector is empty.
    clear()Removes all elements from the vector.

    Examples#

    1. Adding and Printing Elements#

    import java.util.*; public class VectorAddExample { public static void main(String[] args) { Vector<String> vector = new Vector<>(); vector.add("Dog"); vector.add("Cat"); vector.add("Rabbit"); System.out.println("Vector Elements: " + vector); } }

    Output:

    Vector Elements: [Dog, Cat, Rabbit]

    2. Accessing and Modifying Elements#

    import java.util.*; public class VectorAccessExample { public static void main(String[] args) { Vector<Integer> vector = new Vector<>(); vector.add(10); vector.add(20); vector.add(30); System.out.println("Element at index 1: " + vector.get(1)); vector.set(1, 25); System.out.println("Modified Vector: " + vector); } }

    Output:

    Element at index 1: 20 Modified Vector: [10, 25, 30]

    3. Removing Elements#

    import java.util.*; public class VectorRemoveExample { public static void main(String[] args) { Vector<String> vector = new Vector<>(); vector.add("Red"); vector.add("Green"); vector.add("Blue"); vector.remove(1); System.out.println("After removal: " + vector); } }

    Output:

    After removal: [Red, Blue]

    4. Checking Size and Clearing Vector#

    import java.util.*; public class VectorClearExample { public static void main(String[] args) { Vector<Integer> vector = new Vector<>(); vector.add(100); vector.add(200); System.out.println("Size before clear: " + vector.size()); vector.clear(); System.out.println("Is vector empty? " + vector.isEmpty()); } }

    Output:

    Size before clear: 2 Is vector empty? true

    When to Use Vector#

    Use Vector when:

    • You need thread-safe operations.
    • You are working in a multi-threaded environment.

    However, in most modern applications, ArrayList is preferred over Vector for single-threaded environments due to better performance.

    Conclusion#

    The Vector class is a legacy class that is synchronized and maintains insertion order. It's useful when thread safety is required without external synchronization. In the next blog, we will cover Stack, which extends Vector and operates in a LIFO manner.

    Last updated on Apr 09, 2025