Java TreeMap isEmpty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.TreeMap

Definition

In Java, isEmpty() returns true if a TreeMap contains no mappings and false otherwise. It means exactly the same as size() == 0, but reads better.

It matters most before calls that fail on an empty map: firstKey() and lastKey() throw NoSuchElementException when there is nothing to return. Called on a range view, it tells you whether any key falls inside that range.

An instance method of java.util.TreeMap, part of TreeMap since Java 1.2. A TreeMap keeps its key/value pairs sorted by key in a red-black tree, so lookups and updates take O(log n) time; it is not thread-safe.

Syntax

No arguments; returns a boolean.

Java
TreeMap<String, Integer> cart = new TreeMap<>();
boolean empty = cart.isEmpty();   // true
CallWhat it does
map.isEmpty()true when there are no mappings.
!map.isEmpty()"Is there anything at all?"
map.tailMap(k).isEmpty()true when no key is at or above k.

A map whose only key is mapped to null is not empty: the mapping exists.

Library declarations
Java
// java.util.TreeMap<K, V>
public boolean isEmpty()

// Declared in Map<K, V> (implemented in AbstractMap as size() == 0)

Parameters

ParameterDescription
NoneisEmpty() takes no parameters.

Return Value

Return ValueDescription
trueNo mappings (in the map, or in the view's range).
falseAt least one mapping.

Compare with Similar Methods

Checks for "nothing here":

MethodHow it differs
size()Gives the exact count.
firstEntry()Returns null on an empty map instead of throwing.
clear()Makes a map empty.
HashMap.isEmpty()Same check on a HashMap.

Java Examples

Example 1 — Watch a Shopping Cart Fill and Empty

One item goes in and comes out again.

Java
import java.util.TreeMap;

public class TreeMapIsEmptyCart {
    public static void main(String[] args) {
        TreeMap<String, Integer> cart = new TreeMap<>();
        System.out.println("new cart empty? " + cart.isEmpty());

        cart.put("socks", 2);
        System.out.println("after adding socks: " + cart.isEmpty());

        cart.remove("socks");
        System.out.println("after removing socks: " + cart.isEmpty());
    }
}

How It Works

  1. Born empty. new TreeMap<>() has no mappings, so the first answer is true.
  2. One mapping is enough. After socks is added, the cart is no longer empty.
  3. Empty again. Removing the only key brings the size back to 0, and isEmpty() returns true.

Example 2 — Are There Any Severe Alerts?

Alerts are keyed by severity level (one alert per level). tailMap(4) holds levels 4 and up.

Java
import java.util.TreeMap;

public class TreeMapIsEmptyView {
    public static void main(String[] args) {
        TreeMap<Integer, String> alerts = new TreeMap<>();
        alerts.put(2, "low disk");
        alerts.put(3, "slow query");

        System.out.println("any alerts: " + !alerts.isEmpty());
        System.out.println("any level 4+ alerts: " + !alerts.tailMap(4).isEmpty());
        alerts.put(5, "server down");
        System.out.println("any level 4+ alerts: " + !alerts.tailMap(4).isEmpty());
    }
}

How It Works

  1. The map has alerts. Levels 2 and 3 are stored, so !alerts.isEmpty() is true.
  2. Nothing from level 4. tailMap(4) starts at 4, and no key is that high, so the view is empty.
  3. A severe alert arrives. After level 5 is added, the same kind of view is no longer empty.

Example 3 — Guard firstKey() on an Agenda

firstKey() would throw on an empty agenda, so the helper checks isEmpty() first.

Java
import java.util.TreeMap;

public class TreeMapIsEmptyGuard {
    static String earliest(TreeMap<String, String> agenda) {
        if (agenda.isEmpty()) {
            return "nothing planned";
        }
        return agenda.firstKey() + " " + agenda.get(agenda.firstKey());
    }

    public static void main(String[] args) {
        TreeMap<String, String> agenda = new TreeMap<>();
        System.out.println(earliest(agenda));
        agenda.put("14:00", "dentist");
        agenda.put("09:30", "standup");
        System.out.println(earliest(agenda));
        agenda.clear();
        System.out.println("after clear: " + agenda.isEmpty() + ", size() == 0: " + (agenda.size() == 0));
    }
}

How It Works

  1. Safe on an empty agenda. The guard returns early, so firstKey() is never called on an empty map and cannot throw NoSuchElementException.
  2. Earliest time first. With two items, firstKey() is "09:30" (it sorts before "14:00"), and get fetches standup.
  3. clear() empties it. After clear(), isEmpty() and size() == 0 agree.

Frequently Asked Questions

true if the map has no key/value mappings, false otherwise.
Yes. It is implemented that way and is simply easier to read.
firstKey() and lastKey() throw NoSuchElementException on an empty map. firstEntry() and lastEntry() return null instead.
No. A key mapped to null is still a mapping, so isEmpty() returns false.
Yes. It returns true when no key falls in the view's range, and the answer follows later changes to the map.

Next: keySet()

A live, sorted 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.

12 people found this page helpful