This example shows how to remove elements from Vector in Java. This example also shows how to remove elements from Vector using remove, removeElement, and removeElementAt methods.
How to remove elements from Vector in Java?
There are several ways using which we can remove elements from Java Vector as given below.
1. How to remove an element at a specific index from Vector using remove and removeElementAt methods?
The Vector remove
method removes an element located at the specified index of this vector object.
1 |
public E remove(int index) |
It returns the element that was removed from the vector object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Vector; public class VectorRemoveElementsExample { public static void main(String[] args) { Vector<String> vColors = new Vector<String>(); vColors.add("Black"); vColors.add("Orange"); vColors.add("Yellow"); /* * To remove element at specific index from vector, * use the remove method */ //this will remove and return element "Orange" System.out.println( vColors.remove(1) ); System.out.println("Vector contains: " + vColors); } } |
1 2 |
Orange Vector contains: [Black, Yellow] |
The removeElementAt
method is similar to this remove
method and removes an element from the vector at the specified index.
1 |
public void removeElementAt(int index) |
The only difference is in the return type, the remove
method returns the element that was removed while the return type of the removeElementAt
method is void.
2. How to remove a specific element from Vector using the remove and removeElement methods?
The overloaded remove
method takes an object as an argument instead of the index and removes the first occurrence of the specified element from the vector.
1 |
public boolean remove(Object object) |
It returns true if the specified element was found and removed from this vector object, false otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Vector<String> vColors = new Vector<String>(); vColors.add("Black"); vColors.add("Orange"); vColors.add("Yellow"); //this will remove "Orange" and return true System.out.println( vColors.remove("Orange") ); //this will return false as the element "Red" does not exist System.out.println( vColors.remove("Red") ); System.out.println("Vector contains: " + vColors); |
Output
1 2 3 |
true false Vector contains: [Black, Yellow] |
The removeElement
method is similar to this remove
method and removes the first occurrence of the element from this vector object.
1 |
public boolean removeElement(Object object) |
It also returns true if the specified element was found and removed from this vector object, false otherwise.
How to remove an object of a custom class from the Vector?
If the Vector elements are objects of a custom class, then the remove
and removeElement
methods will not work properly if the custom class has not overridden the equals
and hashCode
methods as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Vector; class Student{ private int id; public Student(int id){ this.id = id; } public String toString(){ return "Student[" + this.id + "]"; } } public class VectorRemoveElementsExample { public static void main(String[] args) { Vector<Student> vStudents = new Vector<Student>(); vStudents.add(new Student(201)); vStudents.add(new Student(202)); vStudents.add(new Student(203)); System.out.println( vStudents.remove(new Student(202)) ); } } |
Output
1 |
false |
As we can see from the output, even if the object element exists in the vector, the remove
method returned false. To solve this problem, we need to override the equals
and hashCode
methods in the Student custom class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import java.util.Vector; class Student{ private int id; public Student(int id){ this.id = id; } public String toString(){ return "Student[" + this.id + "]"; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (id != other.id) return false; return true; } } public class VectorRemoveElementsExample { public static void main(String[] args) { Vector<Student> vStudents = new Vector<Student>(); vStudents.add(new Student(201)); vStudents.add(new Student(202)); vStudents.add(new Student(203)); System.out.println( vStudents.remove(new Student(202)) ); System.out.println("Vector contains: " + vStudents); } } |
Output
1 2 |
true Vector contains: [Student[201], Student[203]] |
Please also visit how to clear or remove all elements from the Vector example to know more.
This example is a part of the Vector in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Vector Documentation