Home Java HashMap Methods isEmpty() Java HashMap isEmpty() Method Definition In Java, isEmpty() returns true if a HashMap contains no key/value mappings, and false otherwise. It means exactly the same as size() == 0.
A map holding only a null key, or keys mapped to null, is not empty. And a null variable is not an empty map: calling isEmpty() on it throws NullPointerException.
An instance method of java.util.HashMap (class since Java 1.2): a hash table of key/value pairs with no guaranteed order, one null key, any number of null values, and no thread safety .
Syntax No arguments; returns a boolean.
Map<String, Integer> m = new HashMap<>();
boolean e1 = m.isEmpty(); // true
m.put("x", 1);
boolean e2 = m.isEmpty(); // false Call What it does map.isEmpty()true when there are no mappings.!map.isEmpty()At least one mapping. map == null || map.isEmpty()Null-safe check for a map that may be missing. map.size() == 0Same result, less direct.
isEmpty() takes constant time and never changes the map.
Library declarations // java.util.HashMap<K, V>
public boolean isEmpty()
// Declared in Map<K, V> Parameters Parameter Description None isEmpty() takes no parameters.
Return Value Return Value Description booleantrue if the map contains no mappings; otherwise false.
Important
isEmpty() counts mappings, not "meaningful" values: map.put(null, null) makes it false. Removing the last mapping through a view such as keySet() or values() makes the map empty too, because views write through to the map.
Compare with Similar Methods Emptiness and size:
Method How it differs size()The exact number of mappings. clear()Removes everything, so isEmpty() becomes true. containsKey(k)Checks for one particular key. remove(k)Removing the last key empties the map.
Java Examples 1. Empty or not 2. Guard a report 3. null entries and views Example 1 — A Cart Before and After Changes Watch isEmpty() as one item is added and removed.
import java.util.HashMap;
import java.util.Map;
public class HashMapIsEmptyDemo {
public static void main(String[] args) {
Map<String, Integer> cart = new HashMap<>();
System.out.println("new map empty? " + cart.isEmpty());
cart.put("apple", 3);
System.out.println("after put empty? " + cart.isEmpty());
cart.remove("apple");
System.out.println("after remove empty? " + cart.isEmpty() + ", size " + cart.size());
}
} new map empty? true
after put empty? false
after remove empty? true, size 0 How It Works Nothing yet. A freshly created map has no mappings, so isEmpty() starts out true.One mapping. Adding apple gives the map a size of 1 and flips the answer to false.Back to empty. Removing the only key leaves no mappings, so the map is empty again.Matches the size. The printed size of 0 confirms that isEmpty() is just size() == 0.Example 2 — Print a Summary Only When There Are Orders A helper returns a friendly message for an empty map and a total otherwise.
import java.util.HashMap;
import java.util.Map;
public class HashMapIsEmptyGuard {
public static void main(String[] args) {
Map<String, Integer> orders = new HashMap<>();
System.out.println(summary(orders));
orders.put("pizza", 2);
orders.put("salad", 1);
System.out.println(summary(orders));
}
static String summary(Map<String, Integer> orders) {
if (orders.isEmpty()) {
return "No orders yet";
}
int items = 0;
for (int qty : orders.values()) {
items += qty;
}
return orders.size() + " dishes, " + items + " items";
}
} No orders yet
2 dishes, 3 items How It Works Early return. The helper checks isEmpty() first and returns a message instead of building an empty report.Orders arrive. Two dishes are added, so the second call skips the early return.Sum the quantities. The loop unboxes each value from values() and adds 2 + 1 = 3; the order of the values does not matter for a sum.Two different counts. size() counts dishes (keys), while the loop counts items (the sum of values).Example 3 — A null Mapping Is Still a Mapping Store only null, remove it through the key view, and write a null-safe check for a missing map.
import java.util.HashMap;
import java.util.Map;
public class HashMapIsEmptyNullEntry {
public static void main(String[] args) {
Map<String, String> notes = new HashMap<>();
notes.put(null, null);
System.out.println("only a null key: empty? " + notes.isEmpty() + ", size " + notes.size());
boolean removed = notes.keySet().remove(null);
System.out.println("removed via keySet: " + removed);
System.out.println("now empty? " + notes.isEmpty());
Map<String, String> missing = null;
System.out.println("null-safe check: " + (missing == null || missing.isEmpty()));
}
} only a null key: empty? false, size 1
removed via keySet: true
now empty? true
null-safe check: true How It Works null to null. put(null, null) stores a real mapping, so the map has size 1 and is not empty.Remove through a view. keySet().remove(null) deletes the mapping from the map itself and returns true because something was removed.View changes count. After the view removal the map has no mappings, so isEmpty() is true.No map at all. missing is null; the || short-circuits before isEmpty() is called, avoiding a NullPointerException.Frequently Asked Questions How do I check if a HashMap is empty in Java? Call map.isEmpty(). It returns true when the map has no key/value mappings.
Is isEmpty() the same as size() == 0? Yes. Both give the same answer; isEmpty() reads more clearly.
Is a map with a null key or null value empty? No. put(null, null) adds a real mapping, so isEmpty() returns false.
What happens if I call isEmpty() on a null map? You get a NullPointerException. Use map == null || map.isEmpty() when the map may be missing.
Does clear() make isEmpty() return true? Yes. clear() removes every mapping, so the size becomes 0.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion