What is Vector in Java?

Vector is a legacy class that represents a dynamic array. This class existed before Collection framework and is modified after the introduction of Collection framework to make a compatible to the Collection framework.

Commonly used methods of Vector:

  1. addElement () : It is used to add an element to a Vector.

    public boolean addElement(Object element)
  2. removeElement () : It is used to remove element from Vector.

    public boolean remove(Object element)
  3. capacity () : It returns the capacity of a Vector.

    public int capacity()
  4. ensureCapacity () : It is used to increase the capacity of a Vector.

    public void ensureCapacity(int capacity)
  5. elements () : It is used to obtain Enumeration for traversing the Vector elements.

    public Enumeration elements()

Enumeration is legacy interface that is used by collection to facilitate traversing there objects in implementation independent manner.

Methods of Enumeration Interface:

  1. hasMoreElements () : It is used to find out whether an element is collection or not.

    public boolean hasMoreElements()
  2. nextElement () : It is used to obtain reference of next element of an collection.

    Public Object nextElement()

JbkEmployee.java

package com.javabykiran.vector;

public class JbkEmployee {
    String name;
    String job;
    int salary;
    public JbkEmployee(String name, String job, int salary) {
	super();
	this.name = name;
	this.job = job;
	this.salary = salary;
    }

    public void display() {
	System.out.println("Name -> " + name + " job -> " + job + " salary -> " + salary);
    }

    public boolean equals(Object o) {
        JbkEmployee e1 = (JbkEmployee) o;
	return this.name.equals(e1.name) && this.job.equals(e1.job) && this.salary == e1.salary;
    }
}

JbkVectorDemo.java

package com.javabykiran.vector;
import java.util.Enumeration;
import java.util.Vector;

public class JbkVectorDemo {
    public static void main(String[] args) {
	Vector v = new Vector<>();

	v.add(new JbkEmployee("JavaByKiran", "JavaTrainer", 15000));
	v.add(new JbkEmployee("JavaByKiran", "SeleniumTrainer", 15000));
	v.add(new JbkEmployee("JavaByKiran", "PythonTrainer", 15000));

	System.out.println("Number of Element in Vector : " + v.size());

	Enumeration en = v.elements();

	while (en.hasMoreElements()) {
            JbkEmployee e1 = (JbkEmployee) en.nextElement();
            System.out.println(e1.hashCode() + "\t");
            e1.display();
	}
	System.out.println("Following object is search in the vector");
	JbkEmployee e2 = new JbkEmployee("JavaByKiran", "JavaTrainer", 15000);
	System.out.println(e2.hashCode() + "\t");
	e2.display();
	System.out.println("Search result is :" + v.contains(e2));

	System.out.println("Removing following object in the vector");
	JbkEmployee e3 = new JbkEmployee("JavaByKiran", "SeleniumTrainer", 15000);
	System.out.println(e3.hashCode() + "\t");
	e3.display();
	System.out.println("Removing elements is :" + v.remove(e3));
	System.out.println("After removing number of Elements in vector :" + v.size());
    }
}