Collections.checkedMap(Map, Class, Class): This method is available in java.util.Collections class of Java.
Syntax:
<E1, E2> Map<E1, E2> java.util.Collections.checkedMap(Map<E1, E2> m, Class<E1> keyType, Class<E2> valueType)
This method takes three arguments. This method returns a dynamical type safe view of the specified 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.HashMap;import java.util.Map;public class CollectionscheckedMap {public static void main(String[] args) {Map<String, Integer> map = new HashMap<String,Integer>();map.put("Hello", 1);map.put("Jave", 2);System.out.println(Collections.checkedMap(map,String.class, Integer.class));}}
Output:
{Hello=1, Jave=2}
No comments:
Post a Comment