Java HashSet isEmpty() Method

16 Dec 2024 | 2 min read

The isEmpty() method of Java HashSet class is used to check wheather HashSet contains an element or not. It returns true if the set contains element, otherwise returns false.

Syntax

Following is the declaration of isEmpty() method:

Parameter

This method does not accept any parameter.

Returns

The isEmpty() method returns true if this set contains no elements.

Exceptions

NA

Compatibility Version

Java 1.2 and above

Example 1

Output:

HashSet Elements: [Welcome, To, JavaTpoint] Is the set empty: false Is the set empty: true 

Example 2

Output:

Is hash set empty?:- false HashSet Element: [98, 67, 45] Is hash set empty?:- true HashSet Element: [] 

Example 3

Output:

student database has 3 names: [Rahul, Mohan, Karan] student database is empty: []