Home Java TreeMap Methods isEmpty() Java TreeMap isEmpty() Method 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.
TreeMap<String, Integer> cart = new TreeMap<>();
boolean empty = cart.isEmpty(); // true Call What 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.util.TreeMap<K, V>
public boolean isEmpty()
// Declared in Map<K, V> (implemented in AbstractMap as size() == 0) Parameters Parameter Description None isEmpty() takes no parameters.
Return Value Return Value Description trueNo mappings (in the map, or in the view's range). falseAt least one mapping.
Important
Check isEmpty() before firstKey() or lastKey() to avoid NoSuchElementException, or use firstEntry() , which returns null instead. On a view, the answer changes as the map changes.
Compare with Similar Methods Checks for "nothing here":
Java Examples 1. Empty, full, empty 2. Empty range view 3. Guard firstKey() Example 1 — Watch a Shopping Cart Fill and Empty One item goes in and comes out again.
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());
}
} new cart empty? true
after adding socks: false
after removing socks: true How It Works Born empty. new TreeMap<>() has no mappings, so the first answer is true.One mapping is enough. After socks is added, the cart is no longer empty.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.
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());
}
} any alerts: true
any level 4+ alerts: false
any level 4+ alerts: true How It Works The map has alerts. Levels 2 and 3 are stored, so !alerts.isEmpty() is true.Nothing from level 4. tailMap(4) starts at 4, and no key is that high, so the view is empty.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.
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));
}
} nothing planned
09:30 standup
after clear: true, size() == 0: true How It Works Safe on an empty agenda. The guard returns early, so firstKey() is never called on an empty map and cannot throw NoSuchElementException.Earliest time first. With two items, firstKey() is "09:30" (it sorts before "14:00"), and get fetches standup.clear() empties it. After clear(), isEmpty() and size() == 0 agree.Frequently Asked Questions What does TreeMap isEmpty() return? true if the map has no key/value mappings, false otherwise.
Is isEmpty() the same as size() == 0? Yes. It is implemented that way and is simply easier to read.
Why check isEmpty() before firstKey()? firstKey() and lastKey() throw NoSuchElementException on an empty map. firstEntry() and lastEntry() return null instead.
Is a map with a null value empty? No. A key mapped to null is still a mapping, so isEmpty() returns false.
Can I call isEmpty() on headMap or tailMap? Yes. It returns true when no key falls in the view's range, and the answer follows later changes to the map.
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.
12 people found this page helpful
Helpful Share Copy link Suggestion