VOOZH about

URL: https://www.geeksforgeeks.org/java/vector-contains-method-in-java/

⇱ Java Vector contains() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Vector contains() Method

Last Updated : 16 Jan, 2025

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.


Output
Vector: [Welcome, To, Geeks, 4, Geeks]
Contains 'Geeks'? true
Contains '4'? true
Contains 'No'? false

Syntax of Vector contains() Method

boolean contains(Object obj)

Parameters: obj: The element to be checked for its presence in vector.

Return Type:

  • It returns true, if the element is present in the vector
  • It returns false, if the element is not present in the vector 

Example 2: In this example, we will check whether a particular integer element is present in the vector or not.


Output
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.


Output
Vector: [Person{name='Geek1', age=25}, Person{name='Geek2', age=30}, Person{name='Geek3', age=35}]
Contains Geek1? true
Contains Geek5? false
Comment