This example shows how to check if the element exists in Vector in Java. This example also shows how to check if the custom class object element exists in vector using the contains method.
How to check if the element exists in Vector in Java?
The Vector contains
method returns true if the specified element exists in this vector object.
1 |
public boolean contains(Object element) |
It returns false if the vector does not contain the specified element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Vector; public class CheckIfElementExistsInVector { public static void main(String[] args) { Vector<String> vColors = new Vector<String>(); vColors.add("Black"); vColors.add("Orange"); vColors.add("Yellow"); /* * To check if element exists in vector, use * the contains method */ //this will return true as vector contains element "Orange" System.out.println( vColors.contains("Orange") ); //this will return false as the element "Red" does not exist System.out.println( vColors.contains("Red") ); } } |
Output
1 2 |
true false |
How to check if the custom class object element exists in the Vector?
Let’s see what happens when we check for the element that is an object of a custom class using the contains
method.
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 CheckIfElementExistsInVector { public static void main(String[] args) { Vector<Student> vStudents = new Vector<Student>(); vStudents.add(new Student(1001)); vStudents.add(new Student(1002)); vStudents.add(new Student(1003)); System.out.println( vStudents.contains( new Student(1002) ) ); } } |
Output
1 |
false |
So even if the specified object was present in the vector object, the contains
method returned false.
That is because the contains
method relies on the equals
method of the element object. Since the Student class has not overridden the equals
method, the equals
method inherited from the Object class was used that compares the object references, not the actual objects.
We need to override the equals
method (and hashCode
method too) for the contains
method to search and compare the specified object in the vector. Let’s override these methods in the Student class and try again.
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 |
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 CheckIfElementExistsInVector { public static void main(String[] args) { Vector<Student> vStudents = new Vector<Student>(); vStudents.add(new Student(1001)); vStudents.add(new Student(1002)); vStudents.add(new Student(1003)); System.out.println( vStudents.contains( new Student(1002) ) ); } } |
Output
1 |
true |
As we can see from the output, the contains
method worked for the custom class object as expected this time.
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