VOOZH about

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

⇱ Java HashSet contains() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java HashSet contains() Method

Last Updated : 11 Jul, 2025

The contains() method of the HashSet class in Java is used to check if a specific element is present in the HashSet or not. So, mainly it is used to check if a set contains any particular element.


Output
HashSet:[1, 2, 3]
HashSet Contains 2: true
HashSet Contains 10: false

Syntax of HashSet contains() Method

boolean contains(Object o)

  • Parameter: The object "o" to be checked for presence in the HashSet.
  • Return Type: This method returns "true" if the specified element is present in the set otherwise, it returns "false".
Comment