- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 if a HashMap is empty in Java
Use the isEmpty() method to check of a HashMap is empty or not. Let us first create the HashMap −
HashMap hm = new HashMap();
Now, add some elements −
hm.put("Bag", new Integer(1100)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));
Since, we added elements above, therefore, the HashMap isn’t empty. Let us check −
set.isEmpty()
The following is an example to check if a HashMap is empty or not −
Example
import java.util.*; public class Demo { public static void main(String args[]) { // Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); // Get a set of the entries Set set = hm.entrySet(); // Get an iterator Iterator i = set.iterator(); // Display elements while(i.hasNext()) { Map.Entry me = (Map.Entry)i.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } System.out.println(); System.out.println("Is the HashMap with empty elements? "+set.isEmpty()); } }
Output
Belt: 600 Wallet: 700 Bag: 1100 Is the HashMap with empty elements? False
- Related Articles
- Check if a directory is not empty in Java
- Check if a given key exists in Java HashMap
- Check if a String is empty ("") or null in Java
- How to check if String is empty in Java?
- Check if a String is whitespace, empty ("") or null in Java
- Java Program to check if a given value exists in a HashMap
- Check if a String is not empty ("") and not null in Java
- Java Program to check if a string is empty or not
- Java Program to Check if a String is Empty or Null
- Check if a list is not empty in MongoDB?
- Check if value is empty in JavaScript
- Golang to check if a slice is empty
- Python - Check if dictionary is empty
- What is the correct way to check if String is empty in Java?
- How to Check if an Array is Empty or Not in Java

Advertisements