Collections.checkedSortedMap(SortedMap, Class, Class): This method is available in java.util.Collections class of Java.
Syntax:
<E1, E2> SortedMap<E1, E2> java.util.Collections.checkedSortedMap(SortedMap<E1, E2> m, Class<E1> keyType, Class<E2> valueType)
This method takes three arguments. This method returns a dynamically typesafe view of the specified sorted map.
Note: Any attempt to insert a mapping whose key or value has the wrong type will result in an immediate ClassCastException.
Parameters: Three parameters are required for this method.
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.
Returns: a dynamically typesafe view of the specified map.
Exceptions: NA
Approach
Java
import java.util.Collections;import java.util.SortedMap;import java.util.TreeMap;public class CollectionscheckedSortedMap {public static void main(String[] args) {SortedMap<String, Integer> sortedMap = new TreeMap<String,Integer>();sortedMap.put("Hello", 78);sortedMap.put("Java", 23);sortedMap.put("Abc", 24);System.out.println(Collections.checkedSortedMap(sortedMap,String.class, Integer.class));}}
Output:
{Abc=24, Hello=78, Java=23}
No comments:
Post a Comment