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:
addElement () : It is used to add an element to a Vector.
public boolean addElement(Object element)
removeElement () : It is used to remove element from Vector.
public boolean remove(Object element)
capacity () : It returns the capacity of a Vector.
public int capacity()
ensureCapacity () : It is used to increase the capacity of a Vector.
public void ensureCapacity(int capacity)
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:
hasMoreElements () : It is used to find out whether an element is collection or not.
public boolean hasMoreElements()
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());
}
}
Output:
Number of Element in Vector : 3
366712642
Name -> JavaByKiran job -> JavaTrainer salary -> 15000
1829164700
Name -> JavaByKiran job -> SeleniumTrainer salary -> 15000
2018699554
Name -> JavaByKiran job -> PythonTrainer salary -> 15000
Following object is search in the vector
1311053135
Name -> JavaByKiran job -> JavaTrainer salary -> 15000
Search result is :true
Removing following object in the vector
118352462
Name -> JavaByKiran job -> SeleniumTrainer salary -> 15000
Removing elements is :true
After removing number of Elements in vector :2