The checkedMap() method of java.util.Collections class is used to return a dynamically typesafe view of the specified map.
The returned map will be serializable if the specified map is serializable.
Since null is considered to be a value of any reference type, the returned map permits insertion of null keys or values whenever the backing map does.
Syntax:
public static Map checkedMap(Map m, Class keyType, Class valueType)
Parameters: This method takes the following argument as a parameter:
- 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
Return Value: This method returns a dynamically typesafe view of the specified map.
Below are the examples to illustrate the checkedMap() method
Example 1:
// Java program to demonstrate // checkedMap() method // for <String, String> value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of HashMap<String> HashMap<String, String> hmap = new HashMap<String, String>(); // Adding elemnet to hmap hmap.put( "Ram" , "Shyam" ); hmap.put( "Karan" , "Arjun" ); hmap.put( "Karn" , "Veer" ); hmap.put( "duryodhan" , "dhrupat" ); // printing the map System.out.println( "Map: \n" + hmap); // create typesafe view of the specified map // using checkedMap() method Map<String, String> tsmap = Collections .checkedMap(hmap, String. class , String. class ); // printing the typesafe view of specified list System.out.println( "Typesafe view of Map: " + tsmap); } catch (IllegalArgumentException e) { System.out.println( "Exception thrown : \n" + e); } } } |
Map: {Karn=Veer, Karan=Arjun, duryodhan=dhrupat, Ram=Shyam} Typesafe view of Map: {Karn=Veer, Karan=Arjun, duryodhan=dhrupat, Ram=Shyam}
Example 2:
// Java program to demonstrate // checkedMap() method // for <String, Integer> value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of HashMap<String> HashMap<String, Integer> hmap = new HashMap<String, Integer>(); // Adding elemnet to hmap hmap.put( "Player-1" , 20 ); hmap.put( "Player-2" , 30 ); hmap.put( "Player-3" , 40 ); // printing the map System.out.println( "Map: \n" + hmap); // create typesafe view of the specified map // using checkedMap() method Map<String, Integer> tsmap = Collections .checkedMap(hmap, String. class , Integer. class ); // printing the typesafe view of specified list System.out.println( "Typesafe view of Map: \n" + tsmap); } catch (IllegalArgumentException e) { System.out.println( "Exception thrown : " + e); } } } |
Map: {Player-1=20, Player-3=40, Player-2=30} Typesafe view of Map: {Player-1=20, Player-3=40, Player-2=30}
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.