- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check whether a HashSet is empty or not in Java
To check whether a HashSet is empty or not, use the isEmpty() method.
Create a HashSet −
HashSet hs = new HashSet();
Add elements to the HashSet −
hs.add("B"); hs.add("A"); hs.add("D"); hs.add("E"); hs.add("C"); hs.add("F"); hs.add("K"); hs.add("M"); hs.add("N");
Now, check whether the HashSet is empty or not. Since we added elements above, it won’t be empty −
hs.isEmpty();
The following is an example that checks whether a HashSet is empty or not −
Example
import java.util.*; public class Demo { public static void main(String args[]) { // create a hash set HashSet hs = new HashSet(); // add elements to the hash set hs.add("B"); hs.add("A"); hs.add("D"); hs.add("E"); hs.add("C"); hs.add("F"); hs.add("K"); hs.add("M"); hs.add("N"); System.out.println("Elements: "+hs); System.out.println("Is the set empty? = "+hs.isEmpty()); } }
Output
Elements: [A, B, C, D, E, F, K, M, N] Is the set empty? = false
Let us see another example −
Example
import java.util.*; public class Demo { public static void main(String args[]) { HashSet hs = new HashSet(); // empty set System.out.println("Is the set empty? = "+hs.isEmpty()); } }
Output
Is the set empty? = true
- Related Articles
- Check whether a Stack is empty or not in Java
- Check whether a NavigableMap empty or not in Java
- Check whether IdentityHashMap empty or not in Java?
- C# program to check whether a list is empty or not
- Python program to check whether a list is empty or not?
- Check whether a character is Lowercase or not in Java
- Check whether a character is Uppercase or not in Java
- Java Program to check if a string is empty or not
- Check whether a field is empty or null in MySQL?
- Check Whether a Number is a Coprime Number or Not in Java
- Check whether the file is hidden or not in Java
- Check whether two HashSet are equal in Java
- Check whether the entered value is a digit or not in Java
- Check whether the entered value is a letter or not in Java
- Check Whether a Number is an Autobiographical Number or Not in JAVA

Advertisements