Collections checkedSortedSet() method in Java with Examples

Last Updated : 1 Aug, 2026

The checkedSortedSet() method of the java.util.Collections class returns a dynamically type-safe view of the specified SortedSet. It performs runtime type checking, ensuring that only elements of the specified type can be added to the set. If an element of an incompatible type is inserted, a ClassCastException is thrown immediately.

  • Maintains the natural or comparator-based sorted order of elements.
  • Changes made through the returned set are reflected in the original set.
  • The returned set is serializable if the backing set is serializable.
Java
import java.util.*;

public class GFG {
    public static void main(String[] args) {

        SortedSet<String> languages = new TreeSet<>();
        languages.add("Java");
        languages.add("Python");
        languages.add("C++");

        SortedSet<String> checkedSet =
                Collections.checkedSortedSet(languages, String.class);

        System.out.println(checkedSet);
    }
}

Output
[C++, Java, Python]

Syntax 

public static <E> SortedSet<E> checkedSortedSet(
SortedSet<E> s, Class<E> type)

Parameters: This method takes the following argument as a parameter: 

  • s: the sorted set for which a dynamically typesafe view is to be returned
  • type: the type of element that s is permitted to hold

Return Value: This method returns a dynamically typesafe view of the specified sorted set.

Example: Creating a Type-Safe SortedSet of Strings

Java
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;

public class GFG {

    public static void main(String[] args) {

        SortedSet<String> set = new TreeSet<>();

        set.add("Ram");
        set.add("Gopal");
        set.add("Verma");

        System.out.println("Original SortedSet: " + set);

        SortedSet<String> checkedSet =
                Collections.checkedSortedSet(set, String.class);

        System.out.println("Type-safe SortedSet: " + checkedSet);
    }
}

Output
Original SortedSet: [Gopal, Ram, Verma]
Type-safe SortedSet: [Gopal, Ram, Verma]

Explanation: This example creates a SortedSet<String> and wraps it using Collections.checkedSortedSet(). The returned set accepts only String elements while preserving the sorted order.

Example: Creating a Type-Safe SortedSet of Integers

Java
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;

public class GFG {

    public static void main(String[] args) {

        SortedSet<Integer> set = new TreeSet<>();

        set.add(20);
        set.add(30);
        set.add(40);

        System.out.println("Original SortedSet: " + set);

        SortedSet<Integer> checkedSet =
                Collections.checkedSortedSet(set, Integer.class);

        System.out.println("Type-safe SortedSet: " + checkedSet);
    }
}

Output
Original SortedSet: [20, 30, 40]
Type-safe SortedSet: [20, 30, 40]

Explanation: This example creates a SortedSet<Integer> and returns its type-safe view. The wrapper ensures that only Integer objects can be added at runtime.

checkedSortedSet() and checkedNavigableSet()

FeaturecheckedSortedSet()checkedNavigableSet()
ReturnsType-safe SortedSetType-safe NavigableSet
OrderingMaintains sorted orderMaintains sorted order with navigation support
Navigation MethodsNot availableSupports lower(), higher(), floor(), ceiling(), pollFirst(), pollLast()
Use CaseWhen only sorted ordering is requiredWhen sorted ordering and navigation operations are required

Advantages

  • Provides runtime type safety.
  • Prevents accidental insertion of invalid element types.
  • Preserves the sorted order of elements.
  • Reflects changes in the original SortedSet.
Comment