The Collections.checkedNavigableMap() method in Java returns a dynamically type-safe view of the specified NavigableMap. It ensures that only keys and values of the specified types can be added to the map. If an element of an incorrect type is inserted, a ClassCastException is thrown at runtime.
- Useful for maintaining type safety when working with legacy or mixed-type code.
- Changes made through the checked map are reflected in the original backing map.
import java.util.*;
public class CheckedNavigableMapExample {
public static void main(String[] args) {
NavigableMap<String, Integer> marks = new TreeMap<>();
NavigableMap<String, Integer> safeMap =
Collections.checkedNavigableMap(
marks, String.class, Integer.class);
safeMap.put("Alice", 95);
safeMap.put("Bob", 88);
System.out.println(safeMap);
}
}
Output
{Alice=95, Bob=88}
Explanation: This example creates a runtime type-safe NavigableMap. Since only String keys and Integer values are allowed, the map prevents insertion of incompatible types.
Syntax
public static <K, V> NavigableMap<K, V> checkedNavigableMap(
NavigableMap<K, V> map,
Class<K> keyType,
Class<V> valueType
)
Parameters
- map –> The NavigableMap for which a type-safe view is created.
- keyType –> The class object representing the allowed key type.
- valueType –> The class object representing the allowed value type.
Return Value: Returns a runtime type-safe view of the specified NavigableMap.
Exception: ClassCastException – Thrown if an element with an incompatible key or value type is inserted into the checked map.
Example: Creating a Type-Safe NavigableMap with Integer Values
import java.util.*;
public class GFG {
// main method
public static void main(String[] args)
{
// createa tree map
NavigableMap<String, Integer> data
= new TreeMap<>();
// Insert values in the given map
data.put("id1", 56);
data.put("id2", 15);
data.put("id3", 19);
data.put("id4", 70);
// Create type safe view of the given Map
System.out.println(Collections.checkedNavigableMap(
data, String.class, Integer.class));
}
}
Output
{id1=56, id2=15, id3=19, id4=70}Explanation: This example creates a TreeMap with String keys and Integer values. The checkedNavigableMap() method returns a runtime type-safe view, ensuring that only String keys and Integer values can be added.
Example: Creating a Type-Safe NavigableMap with String Values
import java.util.*;
public class GFG {
// main method
public static void main(String[] args)
{
// createa tree map
NavigableMap<String, String> data = new TreeMap<>();
// Insert values in the given map
data.put("id1", "sravan");
data.put("id2", "manoj");
data.put("id3", "sai");
data.put("id4", "vignesh");
// Create type safe view of the given Map
System.out.println(Collections.checkedNavigableMap(
data, String.class, String.class));
}
}
Output
{id1=sravan, id2=manoj, id3=sai, id4=vignesh}
Explanation: This example creates a TreeMap with both keys and values of type String. The returned checked map accepts only String keys and String values, preventing insertion of incompatible data types.
Advantages
- Prevents insertion of invalid key or value types at runtime.
- Helps detect type errors immediately with
ClassCastException. - Useful when working with legacy or mixed-type collections.
- Improves the reliability and safety of collection operations.