Java Collections checkedSortedMap() Method

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

ParameterDescriptionRequired/Optional
mIt is the map for which a dynamically typesafe view is to be returned.Required
keyTypeIt is the type of key that map m is permitted to hold.Required
valueTypeIt 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

Test it Now

Output:

Type safe view of the Sorted Map is: {ABCD=1200, Hindi100=500, JavaTpoint=1100, SSSIT=1300}

Example 2

Test it Now

Output:

Type safe view of the Sorted Map is: {500=Hindi100, 900=ABCD, 1100=JavaTpoint, 1800=SSSIT}

Example 3

Test it Now

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)





Latest Courses