What is the vector.contains method in Java?
The contains() method of the Vector class can be used to check if an element is present in the Vector object.
Syntax
public boolean contains(Object o)
Return value
This method returns true if the passed element present in the vector object. Otherwise, it returns false.
The element is matched using the equals method. Internally, the element and passed argument is matched using:
(argument==null ? element == null : argument.equals(element))
Code
import java.util.Vector;class VectorContainsElementExample {public static void main( String args[] ) {// Creating VectorVector<Integer> vector = new Vector<>();// add elememtsvector.add(1);vector.add(2);vector.add(3);System.out.println("The vector is : " + vector);System.out.println("Checking if 1 is present : " + vector.contains(1));System.out.println("Checking if 2 is present : " + vector.contains(2));System.out.println("Checking if 5 is present : " + vector.contains(5));}}
Explanation
In the code above, we created a Vector object and added elements 1, 2, 3 to it using the add method. We then used the contains(index) method to check if the element is present in the Vector object.
For the value 1 and 2 the contains method returns true. For the value 5 it returns false because 5 is not present in the Vector object.