In Java, Collections.checkedSortedSet(s, type) returns a SortedSet wrapper that type-checks every added element and keeps the sorted-set features: first(), last(), comparator(), and range views.
You might expect a TreeSet to catch wrong types on its own because it compares elements. It often does, but not always: the very first element of an empty TreeSet is only compared with itself, so a wrong type can slip in. The checked wrapper closes that gap.
A static method of the java.util.Collections utility class, available since Java 5. Call it on the class name, as in Collections.checkedSortedSet(...), after import java.util.Collections;. You never create a Collections object.
Syntax
Pass the sorted set and the element class.
Java
SortedSet<Integer> ids = Collections.checkedSortedSet(new TreeSet<Integer>(), Integer.class);
SortedSet raw = ids;
raw.add("7"); // ClassCastException, even when the set is empty
Call
What it does
raw.add(x)
Checked before the tree sees it.
headSet / tailSet / subSet
Return checked views.
first() / last() / comparator()
Same as the wrapped set.
Added in Java 5. Range views returned by the wrapper are themselves checked sorted sets, so they reject wrong types as well as out-of-range values.
Library declarations
Java
// java.util.Collections
public static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s, Class<E> type)
Checked view.headSet(10) on the wrapper returns a checked sorted set, so the text "5" is refused with ClassCastException.
Right type, wrong range. 50 is an Integer, but it lies outside "below 10", so the TreeSet view throws IllegalArgumentException.
Valid insert. 5 passes both checks and appears in the full set between 3 and 8.
Example 3 — Custom Order Still Works Through the Wrapper
Wrap a reverse-ordered TreeSet and confirm that ordering and the comparator survive.
Java
import java.util.Arrays;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
public class CollectionsCheckedSortedSetOrder {
public static void main(String[] args) {
SortedSet<String> desc = Collections.checkedSortedSet(
new TreeSet<String>(Collections.<String>reverseOrder()), String.class);
desc.addAll(Arrays.asList("b", "a", "c"));
System.out.println(desc + ", first " + desc.first() + ", last " + desc.last());
System.out.println("comparator reverses? " + (desc.comparator().compare("a", "b") > 0));
}
}
Output
[c, b, a], first c, last a
comparator reverses? true
How It Works
Checked addAll. All three letters are strings, so the batch passes the type check.
Order from the tree. The reverse comparator puts c first and a last.
Comparator exposed.comparator() returns the tree's comparator, which ranks "a" after "b" (a positive result).
Frequently Asked Questions
It returns a SortedSet view that throws ClassCastException when an element of the wrong type is added.
Usually, through comparison errors, but the first element of an empty TreeSet is only compared with itself, so a wrong type can get in. The wrapper checks every element.
Yes. Range views of the wrapper are checked sorted sets.
ClassCastException means the type is wrong; IllegalArgumentException from a range view means the value is outside its bounds.