checkedSortedMap()
Java Collections checkedSortedMap() Method
Last Updated : 17 Mar 2025
The checkedSortedMap() is an inbuilt method of Java Collections class. This method is used to get a dynamically typesafe view of the specified sorted map.
Syntax
Following is the declaration of checkedSortedMap() method:
Parameter
| Parameter | Description | Required/Optional |
|---|---|---|
| m | It is the map for which a dynamically typesafe view is to be returned. | Required |
| keyType | It is the type of key that map m is permitted to hold. | Required |
| valueType | It is the type of value that map m is permitted to hold. | Required |
Returns
The checkedSortedMap() method returns a dynamically typesafe view of the specified Sorted Map.
Exceptions
ClassCastException
Compatibility Version
Java 1.5 and above
Example 1
Output:
Type safe view of the Sorted Map is: {ABCD=1200, Hindi100=500, JavaTpoint=1100, SSSIT=1300}
Example 2
Output:
Type safe view of the Sorted Map is: {500=Hindi100, 900=ABCD, 1100=JavaTpoint, 1800=SSSIT}
Example 3
Output:
Map content: {500=Hindi100, 900=ABCD, 1100=JavaTpoint, 1800=SSSIT}
Exception in thread "main" java.lang.ClassCastException: Attempt to insert class java.lang.Integer value into map with value type class java.lang.String
at java.base/java.util.Collections$CheckedMap.typeCheck(Collections.java:3578)
at java.base/java.util.Collections$CheckedMap.put(Collections.java:3621)
at myPackage.CollectionCheckedSortedMapExample3.main(CollectionCheckedSortedMapExample3.java:15)
Next TopicJava Collections Class