Home Java Collections Methods emptySortedMap() Java Collections emptySortedMap() Method Definition In Java 8 and later, Collections.emptySortedMap() returns an empty SortedMap that cannot be changed. Use it when a method's return type is SortedMap and there is no data.
Lookups miss as on any empty map, the range views headMap, tailMap, and subMap are empty, and firstKey() and lastKey() throw because there is no key.
A static method of the java.util.Collections utility class, available since Java 8. Call it on the class name, as in Collections.emptySortedMap(...), after import java.util.Collections;. You never create a Collections object.
Syntax Call it with no arguments.
SortedMap<String, Double> none = Collections.emptySortedMap(); // {}
Double p = none.get("tea"); // null
String k = none.firstKey(); // throws NoSuchElementException Call What it does Collections.emptySortedMap()Immutable empty SortedMap. map.firstKey() / lastKey()Throw NoSuchElementException on it. map.headMap(k), tailMap(k), subMap(a, b)Empty, read-only range views. map.get(k)Always null.
Since Java 8. It sits between emptyMap() (plain Map) and emptyNavigableMap() (adds floorKey, ceilingEntry, and similar).
Library declarations // java.util.Collections
public static final <K,V> SortedMap<K,V> emptySortedMap() Parameters Parameter Description None emptySortedMap() takes no parameters.
Return Value Return Value Description SortedMap<K,V>An empty, immutable sorted map.
Compare with Similar Methods Related empty and sorted maps:
Java Examples 1. Return from a method 2. firstKey throws 3. Range views Example 1 — An Empty Price List for a New Shop A method returns a shop's prices as a SortedMap. A new shop has none yet.
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;
public class CollectionsEmptySortedMapPrices {
static SortedMap<String, Integer> prices(String shop) {
if (shop.equals("cafe")) {
SortedMap<String, Integer> p = new TreeMap<>();
p.put("tea", 3);
p.put("cake", 5);
return p;
}
return Collections.emptySortedMap();
}
public static void main(String[] args) {
System.out.println("cafe: " + prices("cafe") + " first item " + prices("cafe").firstKey());
SortedMap<String, Integer> fresh = prices("new");
System.out.println("new: " + fresh + " empty " + fresh.isEmpty());
}
} cafe: {cake=5, tea=3} first item cake
new: {} empty true How It Works Sorted by key. The cafe's TreeMap orders items alphabetically, so cake comes before tea and is the first key.Matching return type. For a new shop the method returns emptySortedMap(), which satisfies the SortedMap return type.Safe check. The caller asks isEmpty() instead of calling firstKey() on the empty map.Example 2 — firstKey() and lastKey() on Nothing See the difference between a missing value (get returns null) and a missing key position (firstKey throws).
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.SortedMap;
public class CollectionsEmptySortedMapKeys {
public static void main(String[] args) {
SortedMap<Integer, String> none = Collections.emptySortedMap();
System.out.println("get(1): " + none.get(1));
try {
none.firstKey();
} catch (NoSuchElementException e) {
System.out.println("firstKey: " + e.getClass().getSimpleName());
}
try {
none.lastKey();
} catch (NoSuchElementException e) {
System.out.println("lastKey: " + e.getClass().getSimpleName());
}
}
} get(1): null
firstKey: NoSuchElementException
lastKey: NoSuchElementException How It Works Lookups return null. get(1) simply reports that key 1 has no value.Positions throw. firstKey() must return a real key, and there is none, so it throws NoSuchElementException.Same at the other end. lastKey() fails the same way on an empty sorted map.Example 3 — headMap and tailMap of an Empty Sorted Map Range views of the empty map are empty, and putting through one of them is refused.
import java.util.Collections;
import java.util.SortedMap;
public class CollectionsEmptySortedMapViews {
public static void main(String[] args) {
SortedMap<String, Integer> none = Collections.emptySortedMap();
System.out.println("headMap(m): " + none.headMap("m"));
System.out.println("tailMap(m): " + none.tailMap("m"));
try {
none.headMap("m").put("a", 1);
} catch (UnsupportedOperationException e) {
System.out.println("put through headMap: " + e.getClass().getSimpleName());
}
}
} headMap(m): {}
tailMap(m): {}
put through headMap: UnsupportedOperationException How It Works Keys before m. headMap("m") would hold keys less than "m"; there are none.Keys from m on. tailMap("m") is empty for the same reason.Read-only views. Putting "a" through the head view throws UnsupportedOperationException, just like putting into the map itself.Frequently Asked Questions What does Collections.emptySortedMap() return? An empty, immutable SortedMap. It was added in Java 8.
What does firstKey() do on emptySortedMap()? It throws NoSuchElementException, because the map has no keys.
Does get() throw on an empty sorted map? No. get returns null for any key; only methods that must return an existing key, such as firstKey and lastKey, throw.
When should I use emptySortedMap() instead of emptyMap()? When the declared type is SortedMap, for example the return type of a method, so the result keeps the sorted-map methods.
Can I put entries through headMap() of an empty sorted map? No. The range views are read-only and throw UnsupportedOperationException.
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.
11 people found this page helpful
Helpful Share Copy link Suggestion