Home Java Collections Methods unmodifiableSortedMap() Java Collections unmodifiableSortedMap() Method 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.
SortedMap<String, String> board = Collections.unmodifiableSortedMap(departures);
String first = board.firstKey();
SortedMap<String, String> morning = board.headMap("12:00"); // read-only Call What 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.util.Collections
public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) Parameters Parameter Description mThe sorted map to expose read-only. Must not be null.
Return Value Return Value Description SortedMap<K,V>A read-only view of m.
Important
Changes through the view, its range views, or its key, value, and entry views throw UnsupportedOperationException. firstKey() and lastKey() on an empty map throw NoSuchElementException.
Compare with Similar Methods Related map wrappers:
Java Examples 1. First and last 2. Range views 3. Live and widened Example 1 — First and Last Train of the Day Times stored as "HH:mm" strings sort correctly as keys.
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 Sorted keys. Zero-padded times compare correctly as text, so the TreeMap orders them from morning to evening.Ends of the day. firstKey() and lastKey() pick 08:15 and 17:05, and get finds each destination.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.
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());
}
}
} morning {08:15=Leeds}
afternoon {12:40=York, 17:05=Hull}
clear afternoon: UnsupportedOperationException How It Works Split key need not exist. "12:00" is not a departure; it is just the boundary for headMap and tailMap.Two halves. Only 08:15 is earlier than noon; 12:40 and 17:05 belong to the afternoon slice.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.
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());
}
} first 08:15, name length 5
after owner update: {06:50=Bath, 08:15=Leeds, 12:40=York}
first is now 06:50, size 3 How It Works Wider values. The ? extends V parameter lets a String-valued map be viewed with CharSequence values; "Leeds" has length 5.Owner keeps control. The class holding departures adds a 06:50 train directly.View follows. The new train appears first in the view, and the size grows to 3.Frequently Asked Questions What does Collections.unmodifiableSortedMap() do? It returns a read-only SortedMap view that keeps firstKey, lastKey, comparator, and range views.
Are headMap() and tailMap() of the view modifiable? No. Range views are wrapped as read-only too.
Does the view change when the TreeMap changes? Yes. It reads the original map, so new keys appear in sorted order.
What is the difference from unmodifiableNavigableMap()? The navigable version (Java 8) also has floorEntry, ceilingKey, higherKey, descendingMap, and similar methods.
Why does the parameter use ? extends V? Because nothing can be inserted, it is safe to view the values as a supertype, such as String values as CharSequence.
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