The Collections.checkedNavigableSet() method in Java returns a dynamically type-safe view of the specified NavigableSet. It ensures that only elements of the specified type can be added to the set. If an element of an incorrect type is inserted, a ClassCastException is thrown at runtime.
- Helps maintain type safety when working with legacy or mixed-type code.
- Changes made through the checked set are reflected in the original backing set.
import java.util.*;
public class CheckedNavigableSetDemo {
public static void main(String[] args) {
NavigableSet<String> languages = new TreeSet<>();
languages.add("Java");
languages.add("Python");
languages.add("C++");
NavigableSet<String> checkedSet =
Collections.checkedNavigableSet(languages, String.class);
checkedSet.add("Go");
System.out.println(checkedSet);
}
}
Output
[C++, Go, Java, Python]
Explanation: The program creates a TreeSet, wraps it with checkedNavigableSet(), adds a valid String element, and prints the sorted, type-safe set.
Syntax
public static <E> NavigableSet<E> checkedNavigableSet(
NavigableSet<E> set, Class<E> type)
Parameters
- set: The
NavigableSetfor which a type-safe view is to be created. - type: The
Classobject representing the permitted element type.
Return Value: Returns a dynamically type-safe view of the specified NavigableSet.
Exception: ClassCastException –> Thrown if an element of an incompatible type is added to the checked set.
Example: Creating a Type-Safe NavigableSet of Strings
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create a set of string type
NavigableSet<String> data = new TreeSet<>();
// Insert the values into the set
data.add("java");
data.add("php/jsp");
data.add("python");
data.add("R");
// type safe view of the set
System.out.println(Collections.checkedNavigableSet(
data, String.class));
}
}
Output
[R, java, php/jsp, python]
Explanation: This example creates a TreeSet of String objects and wraps it using Collections.checkedNavigableSet(). The returned set accepts only String elements and displays the elements in sorted order.
Example: Creating a Type-Safe NavigableSet of Integers
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create a set of string type
NavigableSet<Integer> data = new TreeSet<>();
// Insert the values into the set
data.add(1);
data.add(2);
data.add(3);
data.add(4);
// type safe view of the set
System.out.println(Collections.checkedNavigableSet(
data, Integer.class));
}
}
Output
[1, 2, 3, 4]
Explanation: This example creates a TreeSet of Integer objects and obtains a type-safe view using Collections.checkedNavigableSet(). Since all inserted elements are integers, the set is displayed successfully in sorted order.
checkedNavigableSet() vs checkedSet()
| Feature | checkedNavigableSet() | checkedSet() |
|---|---|---|
| Works with | NavigableSet | Any Set |
| Maintains sorted order | Yes | Depends on implementation |
| Navigation methods | Supported (higher(), lower(), ceiling(), floor()) | Not supported |
| Runtime type checking | Yes | Yes |
Advantages
- Provides runtime type safety for NavigableSet.
- Prevents insertion of incompatible element types.
- Useful when working with legacy code that uses raw collections.
- Preserves all operations and ordering of the original NavigableSet