90
Java Collections checkedNavigableMap() Method
The heckedNavigableMap() is an inbuilt method of Java Collections class. This method is used to get a dynamically typesafe view of the specified Navigable Map. If there is any attempt of insert mapping whose key or value has the wrong type, it will result in an immediate ClassCastException.
Syntax
Following is the declaration of heckedNavigableMap() 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 checkedNavigableMap() method returns a dynamically typesafe view of the specified Navigable Map.
Exceptions
ClassCastException
Compatibility Version
Java 1.8 and above.
Example 1
Output:
Type safe view of the Navigable Map is: {A=11, B=12, C=13, V=14}
Example 2
Output:
Navigable Map Element: {1=One, 2=Two, 3=Three} 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.CollectionCheckedNavigableMapExample2.main(CollectionCheckedNavigableMapExample2.java:13)
Example 3
Output:
Type safe view of the Navigable Map1 is: {A=11, B=12, C=13, V=14} Type safe view of the Navigable Map2 is: {11=A, 12=B}
Next TopicJava Collections Class