In Java 8 and later, Collections.emptyNavigableMap() returns an empty NavigableMap that cannot be changed. A NavigableMap is a sorted map with "closest key" methods such as floorKey, ceilingEntry, firstEntry, and descendingMap.
On the empty map, the entry and key navigation methods return null, firstKey() and lastKey() throw NoSuchElementException, and anything that would remove or add an entry throws UnsupportedOperationException.
A static method of the java.util.Collections utility class, available since Java 8. Call it on the class name, as in Collections.emptyNavigableMap(...), after import java.util.Collections;. You never create a Collections object.
Syntax
Call it with no arguments.
Java
NavigableMap<Integer, String> none = Collections.emptyNavigableMap();
Map.Entry<Integer, String> e = none.floorEntry(250); // null
Integer k = none.ceilingKey(5); // null
Call
What it does
Collections.emptyNavigableMap()
Immutable empty NavigableMap.
map.floorEntry(k), ceilingKey(k), firstEntry()
Return null.
map.firstKey() / lastKey()
Throw NoSuchElementException.
map.pollFirstEntry()
Throws UnsupportedOperationException (read-only).
Since Java 8. Its views (descendingMap(), headMap, navigableKeySet(), and so on) are empty and read-only as well.
Library declarations
Java
// java.util.Collections
public static final <K,V> NavigableMap<K,V> emptyNavigableMap()
Poll means remove.pollFirstEntry() would take the first entry out of the map, so the read-only map throws UnsupportedOperationException.
Reversed, still empty.descendingMap() prints {}.
Key view.navigableKeySet() is a set view of the keys, which prints [] here.
Example 3 — Find a Reward Tier, or None
A helper picks the highest reward tier whose threshold is at or below a spending amount. With no tiers configured, it gets the empty map.
Java
import java.util.Collections;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
public class CollectionsEmptyNavigableMapTiers {
static String tierFor(NavigableMap<Integer, String> tiers, int spent) {
Map.Entry<Integer, String> e = tiers.floorEntry(spent);
return e == null ? "no tier" : e.getValue();
}
public static void main(String[] args) {
NavigableMap<Integer, String> tiers = new TreeMap<>();
tiers.put(0, "Bronze");
tiers.put(100, "Silver");
tiers.put(500, "Gold");
System.out.println("250 -> " + tierFor(tiers, 250));
System.out.println("900 -> " + tierFor(tiers, 900));
System.out.println("250 with no tiers -> " + tierFor(Collections.emptyNavigableMap(), 250));
}
}
Output
250 -> Silver
900 -> Gold
250 with no tiers -> no tier
How It Works
Floor lookup.floorEntry(250) finds the greatest threshold not above 250, which is 100, so the tier is Silver.
Above the top. For 900, the floor is 500, giving Gold.
No tiers. On emptyNavigableMap(), floorEntry returns null, and the helper turns that into "no tier".
Frequently Asked Questions
An empty, immutable NavigableMap. It was added in Java 8.
null. So do ceilingEntry, floorKey, ceilingKey, firstEntry, and lastEntry.
firstKey comes from SortedMap and is specified to throw NoSuchElementException on an empty map. firstEntry comes from NavigableMap and returns null instead.
It throws UnsupportedOperationException, because polling removes an entry and the map is read-only.
When a method or field is declared as NavigableMap and there is no data, so callers can still use the navigation methods safely.