Home » Java HashSet isEmpty() Method with Examples

Java HashSet isEmpty() Method with Examples

by Online Tutorials Library

Java HashSet isEmpty() Method

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

Test it Now

Output:

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

Example 2

Test it Now

Output:

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

Example 3

Test it Now

Output:

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

You may also like