Home » Java HashSet contains() Method with Examples

Java HashSet contains() Method with Examples

by Online Tutorials Library

Java HashSet contains() Method

The contains() method of Java HashSet class is used to check if this HashSet contains the specified element or not. It returns true if element is found otherwise, returns false.

Syntax

Following is the declaration of contains() method:

Parameter

Parameter Description Required/Optional
o It is the element whose presence in this set is to be tested. Required

Returns

The contains() method returns true if this set contains the specified element.

Exceptions

NA

Compatibility Version

Java 1.2 and above

Example 1

Test it Now

Output:

Hash set Elements: [21, 151, 11, 110, 15]  Does the Set contains '110'? :- true  Does the Set contains '555'? :- false  

Example 2

Test it Now

Output:

Enter student name: Rishi  Rishi found on student list.  

Example 3

Test it Now

Output:

Elements of the HashSet:  Twitter  Instagram  Facebook  Does the HashSet contains 'Whatsapp'? :- false  Does the HashSet contains 'Facebook'? :- true  

You may also like