The checkedSortedMap() method of the java.util.Collections class returns a dynamically type-safe view of the specified SortedMap. It ensures that only keys and values of the specified types can be inserted into the map. If an element of an incorrect type is added, a ClassCastException is thrown immediately at runtime.
- Prevents insertion of keys or values of the wrong type.
- Reflects changes made to the original (backing) map.
- Available since Java 5.
import java.util.*;
public class Main {
public static void main(String[] args) {
SortedMap<String, Integer> marks = new TreeMap<>();
marks.put("Alice", 90);
marks.put("Bob", 85);
SortedMap<String, Integer> safeMap =
Collections.checkedSortedMap(
marks,
String.class,
Integer.class);
safeMap.put("Charlie", 95);
System.out.println(safeMap);
}
}
Output
{Alice=90, Bob=85, Charlie=95}
Explanation: The program creates a type-safe view of a SortedMap. Since all inserted keys and values match the specified types, the entries are added successfully.
Syntax
public static <K, V> SortedMap<K, V> checkedSortedMap(
SortedMap<K, V> m, Class<K> keyType, Class<V> valueType)
Parameters: This method takes the following argument as a parameter
- m: the map for which a dynamically typesafe view is to be returned
- keyType: the type of key that m is permitted to hold
- valueType: the type of value that m is permitted to hold
Return Value: This method returns a dynamically typesafe view of the specified map.
Exception: ClassCastException – Thrown if an attempt is made to insert a key or value that is not of the specified type.
Example: Creating a Type-Safe SortedMap with String Keys and Values
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;
public class GFG {
public static void main(String[] args) {
// Creating a SortedMap with String keys and String values
SortedMap<String, String> smap = new TreeMap<>();
// Adding entries
smap.put("Ram", "Gopal");
smap.put("Karan", "Arjun");
smap.put("Karn", "Veer");
System.out.println("Original SortedMap: " + smap);
// Creating a type-safe view
SortedMap<String, String> checkedMap =
Collections.checkedSortedMap(
smap,
String.class,
String.class);
System.out.println("Type-safe SortedMap: " + checkedMap);
}
}
Output
Original SortedMap: {Karan=Arjun, Karn=Veer, Ram=Gopal}
Type-safe SortedMap: {Karan=Arjun, Karn=Veer, Ram=Gopal}
Explanation: This example creates a SortedMap<String, String> and wraps it using Collections.checkedSortedMap(). The returned map allows only String keys and String values, ensuring runtime type safety.
Example: Creating a Type-Safe SortedMap with String Keys and Integer Values
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;
public class GFG {
public static void main(String[] args) {
// Creating a SortedMap with String keys and Integer values
SortedMap<String, Integer> smap = new TreeMap<>();
// Adding entries
smap.put("Ram", 20);
smap.put("Karan", 39);
smap.put("Karn", 40);
System.out.println("Original SortedMap: " + smap);
// Creating a type-safe view
SortedMap<String, Integer> checkedMap =
Collections.checkedSortedMap(
smap,
String.class,
Integer.class);
System.out.println("Type-safe SortedMap: " + checkedMap);
}
}
Output
Original SortedMap: {Karan=39, Karn=40, Ram=20}
Type-safe SortedMap: {Karan=39, Karn=40, Ram=20}
Explanation: This example creates a SortedMap<String, Integer> and returns its type-safe view using Collections.checkedSortedMap(). The wrapper ensures that only String keys and Integer values can be added at runtime, helping prevent accidental type mismatches.
checkedSortedMap() and checkedNavigableMap()
| Feature | checkedSortedMap() | checkedNavigableMap() |
|---|---|---|
| Returns | Type-safe SortedMap | Type-safe NavigableMap |
| Ordering | Maintains keys in sorted order | Maintains sorted order with navigation support |
| Navigation Methods | Not available | Supports lowerKey(), higherKey(), floorKey(), ceilingKey(), firstEntry(), lastEntry(), etc. |
| Backing Collection | SortedMap | NavigableMap |
| Use Case | When only sorted ordering is required | When sorted ordering and navigation operations are required |
Advantages
- Prevents insertion of incorrect key or value types at runtime.
- Helps detect type errors early.
- Maintains type safety when using legacy or raw collections.
- Works directly with the original SortedMap.
- Useful when sharing collections across different parts of an application.