Last Updated : 11 Jul, 2025
The HashSet isEmpty() in Java is used to check if the HashSet is empty or not.
Syntax of HashSet isEmpty() Methodboolean isEmpty()
Return Type: This method returns "true" if the HashSet is empty, otherwise returns "false".
Example: This example demonstrates how the isEmpty() method checks whether the given HashSet is empty or not.
Java
// Java program to demonstrate the working of isEmpty()
import java.util.HashSet;
public class Geeks {
public static void main(String[] args)
{
// Create a new HashSet
HashSet<String> hs = new HashSet<>();
System.out.println("HashSet: " + hs);
// Check if the HashSet is empty
System.out.println("Is the HashSet empty? "
+ hs.isEmpty());
// Add elements to the HashSet
hs.add("Geek1");
hs.add("Geek2");
hs.add("Geek3");
System.out.println("Updated HashSet: " + hs);
// Check again if the HashSet is empty
System.out.println("Is the HashSet empty? "
+ hs.isEmpty());
}
}
HashSet: [] Is the HashSet empty? true Updated HashSet: [Geek3, Geek2, Geek1] Is the HashSet empty? false
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4