How to Remove an Element from Vector in Java

In this example, we'll see how to remove an element from Vector using the remove(Object o) for the purpose of removing a specific element.

public boolean remove(Object o): Finds and deletes the first occurrence of the object o. If Vector does not contain this object, the list remains unchanged.

In this code, we are removing two String values from Vector.

import java.util.Vector; 

public class RemoveVector {

public static void main(String[] args) {

Vector< String> vct = new Vector< String> ();
//add elements
vct.add("e1");
vct.add("e2");
vct.add("e3");
vct.add("e4");
vct.add("e5");

System.out.println("Before deletion:");
for(String e:vct)
System.out.println(e);

//remove the "e2" element
vct.remove("e2");
//remove the "e5" element
vct.remove("e5");

System.out.println("\nAfter deletion:");
for(String e:vct)
System.out.println(e);
}
}
Output:

Before deletion:
e1
e2
e3
e4
e5

After deletion:
e1
e3
e4