Java Collections unmodifiableSortedSet() Method

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

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.

Java
SortedSet<Integer> board = Collections.unmodifiableSortedSet(scores);
int best = board.last();
SortedSet<Integer> top = board.tailSet(90);   // also read-only
CallWhat 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
// java.util.Collections
public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s)

Parameters

ParameterDescription
sThe sorted set to expose read-only. Must not be null.

Return Value

Return ValueDescription
SortedSet<T>A read-only view of s.

Compare with Similar Methods

Related sorted wrappers:

MethodHow it differs
unmodifiableNavigableSet(s)Adds ceiling, floor, descending views.
unmodifiableSet(s)Plain set view; no first/last.
emptySortedSet()Read-only sorted set with no elements.

Java Examples

Example 1 — Read the Lowest and Highest Score

Expose a sorted leaderboard; reading the ends works, removing a score does not.

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

How It Works

  1. Sorted for free. The TreeSet stores the scores in ascending order, and the view prints them that way.
  2. Ends of the range. first() gives 61 and last() gives 95 without any loop.
  3. 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.

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

How It Works

  1. tailSet and headSet. tailSet(90) includes 90 and above; headSet(60) holds everything below 60.
  2. Half-open subSet. subSet(72, 95) includes 72 but stops before 95, giving 72 and 90.
  3. 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.

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

How It Works

  1. Custom ordering. The TreeSet ignores case, so "Ann" sorts before "bob" even though uppercase letters normally come first.
  2. Comparator passed through. view.comparator() returns the very same CASE_INSENSITIVE_ORDER object.
  3. Live and sorted. The owner's new name "Abe" appears in the view at its sorted position and becomes first().

Frequently Asked Questions

It returns a read-only SortedSet view that still supports first, last, comparator, and range views.
No. They are wrapped as read-only views as well.
Yes. comparator() returns the original set's comparator.
The navigable version (Java 8) also offers ceiling, floor, higher, lower, and descending views.
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

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.

12 people found this page helpful