Java Collections emptySortedMap() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

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.

Java
SortedMap<String, Double> none = Collections.emptySortedMap();   // {}
Double p = none.get("tea");                                         // null
String k = none.firstKey();                                         // throws NoSuchElementException
CallWhat 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
// java.util.Collections
public static final <K,V> SortedMap<K,V> emptySortedMap()

Parameters

ParameterDescription
NoneemptySortedMap() takes no parameters.

Return Value

Return ValueDescription
SortedMap<K,V>An empty, immutable sorted map.

Compare with Similar Methods

Related empty and sorted maps:

MethodHow it differs
emptyMap()Plain Map type; no firstKey or range views.
emptyNavigableMap()Adds navigation methods that return null.
unmodifiableSortedMap(m)Read-only view of a real sorted map.
emptySortedSet()The same idea for sorted sets.

Java Examples

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.

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

How It Works

  1. Sorted by key. The cafe's TreeMap orders items alphabetically, so cake comes before tea and is the first key.
  2. Matching return type. For a new shop the method returns emptySortedMap(), which satisfies the SortedMap return type.
  3. 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).

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

How It Works

  1. Lookups return null. get(1) simply reports that key 1 has no value.
  2. Positions throw. firstKey() must return a real key, and there is none, so it throws NoSuchElementException.
  3. 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.

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

How It Works

  1. Keys before m. headMap("m") would hold keys less than "m"; there are none.
  2. Keys from m on. tailMap("m") is empty for the same reason.
  3. Read-only views. Putting "a" through the head view throws UnsupportedOperationException, just like putting into the map itself.

Frequently Asked Questions

An empty, immutable SortedMap. It was added in Java 8.
It throws NoSuchElementException, because the map has no keys.
No. get returns null for any key; only methods that must return an existing key, such as firstKey and lastKey, throw.
When the declared type is SortedMap, for example the return type of a method, so the result keeps the sorted-map methods.
No. The range views are read-only and throw UnsupportedOperationException.

Next: emptySortedSet()

Get an immutable empty SortedSet (Java 8+).

emptySortedSet() 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.

11 people found this page helpful