![]() |
VOOZH | about |
The contains() method in Java is used to check whether a specific element is present in the Vector or not.
Example 1: In this example, we will check whether a particular string is present in the vector or not.
Vector: [Welcome, To, Geeks, 4, Geeks] Contains 'Geeks'? true Contains '4'? true Contains 'No'? false
boolean contains(Object obj)
Parameters: obj: The element to be checked for its presence in vector.
Return Type:
Example 2: In this example, we will check whether a particular integer element is present in the vector or not.
Vector: [10, 15, 30, 20, 5] Contains 100? false Contains 30? true
Example 3: The below Java program demonstrates how the contains() method works with custom objects by overriding the equals() method.
Vector: [Person{name='Geek1', age=25}, Person{name='Geek2', age=30}, Person{name='Geek3', age=35}]
Contains Geek1? true
Contains Geek5? false