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.
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
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
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()
| Feature | checkedSortedSet() | checkedNavigableSet() |
|---|---|---|
| Returns | Type-safe SortedSet | Type-safe NavigableSet |
| Ordering | Maintains sorted order | Maintains sorted order with navigation support |
| Navigation Methods | Not available | Supports lower(), higher(), floor(), ceiling(), pollFirst(), pollLast() |
| Use Case | When only sorted ordering is required | When 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.