Java Collections unmodifiableSortedMap() Method

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

Definition

In Java, Collections.unmodifiableSortedMap(m) returns a read-only SortedMap view. You keep the sorted-map extras, firstKey(), lastKey(), comparator(), and the range views, while every change throws UnsupportedOperationException.

It suits data that is naturally ordered by key and shared with readers, such as a timetable, a price list by date, or version history.

A static method of the java.util.Collections utility class, available since Java 1.2. Call it on the class name, as in Collections.unmodifiableSortedMap(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Wrap a TreeMap or any other SortedMap.

Java
SortedMap<String, String> board = Collections.unmodifiableSortedMap(departures);
String first = board.firstKey();
SortedMap<String, String> morning = board.headMap("12:00");   // read-only
CallWhat it does
view.firstKey() / lastKey()Smallest and largest key.
view.headMap(k) / tailMap(k)Read-only ranges below / from k.
view.subMap(a, b)Read-only range of keys from a up to (not including) b.
SortedMap<K, Super> v = unmodifiableSortedMap(m)Values may be viewed as a supertype.

Available since Java 1.2. For floorKey, ceilingEntry, and descending views, use unmodifiableNavigableMap (Java 8).

Library declarations
Java
// java.util.Collections
public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m)

Parameters

ParameterDescription
mThe sorted map to expose read-only. Must not be null.

Return Value

Return ValueDescription
SortedMap<K,V>A read-only view of m.

Compare with Similar Methods

Related map wrappers:

MethodHow it differs
unmodifiableNavigableMap(m)Adds floorEntry, ceilingKey, descending views.
unmodifiableMap(m)Plain map view; no ordering methods.
synchronizedSortedMap(m)Thread-safe, still changeable.

Java Examples

Example 1 — First and Last Train of the Day

Times stored as "HH:mm" strings sort correctly as keys.

Java
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;

public class CollectionsUnmodifiableSortedMapTimes {
    public static void main(String[] args) {
        SortedMap<String, String> departures = new TreeMap<>();
        departures.put("12:40", "York");
        departures.put("08:15", "Leeds");
        departures.put("17:05", "Hull");
        SortedMap<String, String> board = Collections.unmodifiableSortedMap(departures);

        String first = board.firstKey();
        String last = board.lastKey();
        System.out.println("first " + first + " to " + board.get(first));
        System.out.println("last " + last + " to " + board.get(last));
        try {
            board.put("23:59", "Ghost");
        } catch (UnsupportedOperationException e) {
            System.out.println("put: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Sorted keys. Zero-padded times compare correctly as text, so the TreeMap orders them from morning to evening.
  2. Ends of the day. firstKey() and lastKey() pick 08:15 and 17:05, and get finds each destination.
  3. Board is read-only. Adding a late train through the view throws.

Example 2 — Morning and Afternoon Slices

Split the timetable at noon and try to clear one half.

Java
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;

public class CollectionsUnmodifiableSortedMapRanges {
    public static void main(String[] args) {
        SortedMap<String, String> departures = new TreeMap<>();
        departures.put("08:15", "Leeds");
        departures.put("12:40", "York");
        departures.put("17:05", "Hull");
        SortedMap<String, String> board = Collections.unmodifiableSortedMap(departures);

        SortedMap<String, String> morning = board.headMap("12:00");
        SortedMap<String, String> afternoon = board.tailMap("12:00");
        System.out.println("morning " + morning);
        System.out.println("afternoon " + afternoon);
        try {
            afternoon.clear();
        } catch (UnsupportedOperationException e) {
            System.out.println("clear afternoon: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Split key need not exist. "12:00" is not a departure; it is just the boundary for headMap and tailMap.
  2. Two halves. Only 08:15 is earlier than noon; 12:40 and 17:05 belong to the afternoon slice.
  3. Slices protected. clear() on the afternoon range would delete two trains, so it throws.

Example 3 — Owner Updates and a Wider Value Type

View the timetable with CharSequence values, then let the owner add an early train.

Java
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;

public class CollectionsUnmodifiableSortedMapLive {
    public static void main(String[] args) {
        SortedMap<String, String> departures = new TreeMap<>();
        departures.put("08:15", "Leeds");
        departures.put("12:40", "York");
        SortedMap<String, CharSequence> view = Collections.unmodifiableSortedMap(departures);
        System.out.println("first " + view.firstKey() + ", name length " + view.get("08:15").length());

        departures.put("06:50", "Bath");
        System.out.println("after owner update: " + view);
        System.out.println("first is now " + view.firstKey() + ", size " + view.size());
    }
}

How It Works

  1. Wider values. The ? extends V parameter lets a String-valued map be viewed with CharSequence values; "Leeds" has length 5.
  2. Owner keeps control. The class holding departures adds a 06:50 train directly.
  3. View follows. The new train appears first in the view, and the size grows to 3.

Frequently Asked Questions

It returns a read-only SortedMap view that keeps firstKey, lastKey, comparator, and range views.
No. Range views are wrapped as read-only too.
Yes. It reads the original map, so new keys appear in sorted order.
The navigable version (Java 8) also has floorEntry, ceilingKey, higherKey, descendingMap, and similar methods.
Because nothing can be inserted, it is safe to view the values as a supertype, such as String values as CharSequence.

Next: unmodifiableSortedSet()

Wrap a SortedSet in a read-only view, including its range views.

unmodifiableSortedSet() 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