Vector Class in Java

In this tutorial, we will learn about Vector class in Java and how to use it.

We will see how it is different from the ArrayList class, and where to use Arraylist and Vector.

The Vector class is an implementation of the List interface (similar to ArrayList) that allows us to create resizable arrays.

The biggest difference between Vector and ArrayList is that Vector is synchronized and thread-safe where else ArrayList is not a thread-safe collection.

That simply means in a multithreaded environment you should consider using Vector.

How to Create a Vector?

Let’s see how to create Vector in Java:

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

Here, “T” indicates the type of Vector. For example,

// create Integer type Vector
Vector<Integer> vector= new Vector<>();

// create String type Vector
Vector<String> vector= new Vector<>();

Java Vector Declaration:

Below is the Vector class present in the JDK (with few methods):

public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable {
    protected Object[] elementData;
    protected int elementCount;
    ....
    private static final int MAX_ARRAY_SIZE = 2147483639;

    public synchronized boolean add(E var1) {
        ++this.modCount;
        this.ensureCapacityHelper(this.elementCount + 1);
        this.elementData[this.elementCount++] = var1;
        return true;
    }
    .... rest of the code ....

}

Java Vector Class Methods:

Here are some frequently used methods of Vector Class in Java.

1)add()It is used to add an element to the given vector.
2)addAll()It is used to append all of the elements in the specified collection to the end of this Vector.
3)get()It is used to get element from the specified position.
4)capacity()It is used to get the current capacity of this vector.
5)clear()It is used to delete all of the elements from the vector.
6)removeIf()It is used to remove all of the elements from the vector if satisfy the given predicate.
7)contains()It returns true if the vector contains the specified element.
8)sort()It is used to sort the elements of a vector.

How to Perform Operations In Vector?

Let’s see some of the famous operation that can be performed on Vector Class in Java.

Example 1:

public class VectorExample {

    public static void main(String[] args) {

        // creating vector of String type
        Vector<String> obj = new Vector<>();

        // adding elements
        obj.add("Peter");
        obj.add("Olive");
        obj.add("Smith");

        // printing
        System.out.println(obj);

        // removing elements
        obj.remove(1);

        // printing
        System.out.println(obj);

    }
}

Output:

[Peter, Olive, Smith]
[Peter, Smith]

Example 2

public class VectorExample {

    public static void main(String[] args) {

        // creating vector of String type
        Vector<Integer> obj = new Vector<>();

        // adding elements
        obj.add(1);
        obj.add(2);
        obj.add(3);
        obj.add(4);
        obj.add(5);
        obj.add(6);
        obj.add(7);

        if(obj.contains(7)){
            System.out.println("7 is there");
        }

        System.out.println("First Element is " + obj.firstElement());

        System.out.println("Last Element is " + obj.lastElement());

        System.out.println("Capacity is " + obj.capacity());
        
        // using removeIf() method
        // if vector object contains the given value then remove it
        obj.removeIf(x -> x == 6);

        System.out.println("After removeIf() --> " + obj);
    }
}

Output:

7 is there
First Element is 1
Last Element is 7
Capacity is 10
After removeIf() --> [1, 2, 3, 4, 5, 7]

Vector Vs ArrayList

VectorArrayList
Vector is synchronized (thread-safe).ArrayList is non-synchronized (not thread-safe).
Vector is a legacy class.ArrayList is not legacy.
Vector increments 100% of its current size if it
exceeds its capacity.
ArrayList increments 50% of its current size if it exceeds its capacity.
LinkedList is slower being syncronized.ArrayList is faster being non-syncronized.
Vector can use both iterator or enumerator interface to traverse through elements.ArrayList uses iterator interface to traverse through elements.

Note: It is recommended to use ArrayList in place of Vector when you’re working in a single-threaded application because there is no overhead to acquire and release the lock on objects. ArrayList will provide you better performance.

Official Doc: Vector Class in Java (Oracle)

Stack Class in Java

Set Interface in Java

Leave a Comment