Java HashMap isEmpty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
HashMap

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.

Java
Map<String, Integer> m = new HashMap<>();
boolean e1 = m.isEmpty();   // true
m.put("x", 1);
boolean e2 = m.isEmpty();   // false
CallWhat 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
// java.util.HashMap<K, V>
public boolean isEmpty()

// Declared in Map<K, V>

Parameters

ParameterDescription
NoneisEmpty() takes no parameters.

Return Value

Return ValueDescription
booleantrue if the map contains no mappings; otherwise false.

Compare with Similar Methods

Emptiness and size:

MethodHow 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

Example 1 — A Cart Before and After Changes

Watch isEmpty() as one item is added and removed.

Java
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());
    }
}

How It Works

  1. Nothing yet. A freshly created map has no mappings, so isEmpty() starts out true.
  2. One mapping. Adding apple gives the map a size of 1 and flips the answer to false.
  3. Back to empty. Removing the only key leaves no mappings, so the map is empty again.
  4. 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.

Java
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";
    }
}

How It Works

  1. Early return. The helper checks isEmpty() first and returns a message instead of building an empty report.
  2. Orders arrive. Two dishes are added, so the second call skips the early return.
  3. 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.
  4. 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.

Java
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()));
    }
}

How It Works

  1. null to null. put(null, null) stores a real mapping, so the map has size 1 and is not empty.
  2. Remove through a view. keySet().remove(null) deletes the mapping from the map itself and returns true because something was removed.
  3. View changes count. After the view removal the map has no mappings, so isEmpty() is true.
  4. No map at all. missing is null; the || short-circuits before isEmpty() is called, avoiding a NullPointerException.

Frequently Asked Questions

Call map.isEmpty(). It returns true when the map has no key/value mappings.
Yes. Both give the same answer; isEmpty() reads more clearly.
No. put(null, null) adds a real mapping, so isEmpty() returns false.
You get a NullPointerException. Use map == null || map.isEmpty() when the map may be missing.
Yes. clear() removes every mapping, so the size becomes 0.

Next: keySet()

Get a live Set view of all keys.

keySet() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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