Home Java Collections Methods unmodifiableSortedSet() Java Collections unmodifiableSortedSet() Method Definition In Java, Collections.unmodifiableSortedSet(s) returns a read-only SortedSet view. Besides the usual set reads, it keeps the sorted-set methods first(), last(), comparator(), and the range views.
Range views such as headSet, tailSet, and subSet are wrapped as well, so a caller cannot slip a change in through a slice of the set.
A static method of the java.util.Collections utility class, available since Java 1.2. Call it on the class name, as in Collections.unmodifiableSortedSet(...), after import java.util.Collections;. You never create a Collections object.
Syntax Wrap a TreeSet or any other SortedSet.
SortedSet<Integer> board = Collections.unmodifiableSortedSet(scores);
int best = board.last();
SortedSet<Integer> top = board.tailSet(90); // also read-only Call What it does view.first() / view.last()Smallest and largest element. view.headSet(x) / tailSet(x)Read-only range views below / from x. view.subSet(a, b)Read-only range from a (inclusive) to b (exclusive). view.comparator()The original set's comparator, or null for natural order.
Available since Java 1.2. For the extra navigation methods (ceiling, floor, descendingSet) use unmodifiableNavigableSet, added in Java 8.
Library declarations // java.util.Collections
public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) Parameters Parameter Description sThe sorted set to expose read-only. Must not be null.
Return Value Return Value Description SortedSet<T>A read-only view of s.
Compare with Similar Methods Related sorted wrappers:
Java Examples 1. Leaderboard 2. Range views 3. Comparator kept Example 1 — Read the Lowest and Highest Score Expose a sorted leaderboard; reading the ends works, removing a score does not.
import java.util.Arrays;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
public class CollectionsUnmodifiableSortedSetBoard {
public static void main(String[] args) {
SortedSet<Integer> scores = new TreeSet<>(Arrays.asList(70, 95, 88, 61));
SortedSet<Integer> board = Collections.unmodifiableSortedSet(scores);
System.out.println("board: " + board);
System.out.println("lowest " + board.first() + ", highest " + board.last());
try {
board.remove(61);
} catch (UnsupportedOperationException e) {
System.out.println("remove: " + e.getClass().getSimpleName());
}
}
} board: [61, 70, 88, 95]
lowest 61, highest 95
remove: UnsupportedOperationException How It Works Sorted for free. The TreeSet stores the scores in ascending order, and the view prints them that way.Ends of the range. first() gives 61 and last() gives 95 without any loop.Read-only. Deleting the lowest score through the board throws UnsupportedOperationException.Example 2 — Slices of the Set Are Read-Only Too Split grades into honors and failing ranges, and try to remove a grade through one slice.
import java.util.Arrays;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
public class CollectionsUnmodifiableSortedSetRanges {
public static void main(String[] args) {
SortedSet<Integer> grades =
Collections.unmodifiableSortedSet(new TreeSet<>(Arrays.asList(55, 72, 90, 95, 98)));
SortedSet<Integer> honors = grades.tailSet(90);
SortedSet<Integer> failing = grades.headSet(60);
System.out.println("honors " + honors + ", failing " + failing);
System.out.println("72 to 95: " + grades.subSet(72, 95));
try {
honors.remove(95);
} catch (UnsupportedOperationException e) {
System.out.println("remove via tailSet: " + e.getClass().getSimpleName());
}
}
} honors [90, 95, 98], failing [55]
72 to 95: [72, 90]
remove via tailSet: UnsupportedOperationException How It Works tailSet and headSet. tailSet(90) includes 90 and above; headSet(60) holds everything below 60.Half-open subSet. subSet(72, 95) includes 72 but stops before 95, giving 72 and 90.No loophole. The honors slice is wrapped too, so removing 95 through it throws.Example 3 — Custom Order and Live Updates Wrap a case-insensitive set, check its comparator, and let the owner add a name.
import java.util.Arrays;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
public class CollectionsUnmodifiableSortedSetOrder {
public static void main(String[] args) {
SortedSet<String> names = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
names.addAll(Arrays.asList("bob", "Ann", "carl"));
SortedSet<String> view = Collections.unmodifiableSortedSet(names);
System.out.println(view + ", same comparator? " + (view.comparator() == String.CASE_INSENSITIVE_ORDER));
names.add("Abe");
System.out.println("owner added Abe: " + view + ", first = " + view.first());
}
} [Ann, bob, carl], same comparator? true
owner added Abe: [Abe, Ann, bob, carl], first = Abe How It Works Custom ordering. The TreeSet ignores case, so "Ann" sorts before "bob" even though uppercase letters normally come first.Comparator passed through. view.comparator() returns the very same CASE_INSENSITIVE_ORDER object.Live and sorted. The owner's new name "Abe" appears in the view at its sorted position and becomes first().Frequently Asked Questions What does Collections.unmodifiableSortedSet() do? It returns a read-only SortedSet view that still supports first, last, comparator, and range views.
Are headSet(), tailSet(), and subSet() of the view modifiable? No. They are wrapped as read-only views as well.
Does the view keep a custom comparator? Yes. comparator() returns the original set's comparator.
What is the difference between unmodifiableSortedSet() and unmodifiableNavigableSet()? The navigable version (Java 8) also offers ceiling, floor, higher, lower, and descending views.
Will changes to the original TreeSet show in the view? Yes. The view reads the original set, so new elements appear in sorted order.
Back to the method index See all 60 Collections method guides grouped by what they do.
Method index → 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.
12 people found this page helpful
Helpful Share Copy link Suggestion