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

ParameterDescriptionRequired/Optional
oIt 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

Output:

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

Example 2

Output:

Enter student name: Rishi
Rishi found on student list.

Example 3

Output:

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