The checkedMap() method of Collections class been present inside java.util package 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 the 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 3 arguments as a parameter as listed below as follows: Â
- Map for which a dynamically typesafe view is to be returned
- Type of key that m is permitted to hold
- Type of value that m is permitted to hold
Return Value: This method returns a dynamically typesafe view of the specified map.
Exceptions: This method does throw ClassCastExceptionÂ
Tip: This method is compatible with java version 1.5 and onwards.
Now let us see a few examples in order to implement the above method and to get a better understanding of the method which is as follows:Â
Example 1:
Java
// Java program to Demonstrate checkedMap() method // of Collections class Â
// Importing required classes import java.util.*; Â
// Main class public class GFG { Â
    // Main driver method     public static void main(String[] argv) throws Exception     { Â
        // Try block to check for exceptions         try { Â
            // Creating an empty HashMap by declaring             // HashMap key-value pairs             // of string and string type             HashMap<String, String> hmap                 = new HashMap<String, String>(); Â
            // Adding custom key-value pairs to above             // HashMap             hmap.put( "Ram" , "Shyam" );             hmap.put( "Karan" , "Arjun" );             hmap.put( "Karn" , "Veer" );             hmap.put( "duryodhan" , "dhrupat" ); Â
            // Printing all the key-value pairs of above             // HashMap             System.out.println( "Map: \n" + hmap); Â
            // Now creating typesafe view of the specified             // map using checkedMap() method of Collections             // class             Map<String, String> tsmap                 = Collections.checkedMap(hmap, String. class ,                                          String. class ); Â
            // Now printing the typesafe view of specified             // list             System.out.println( "Typesafe view of Map: "                                + tsmap);         } Â
        // Catch block to handle the exceptions         catch (IllegalArgumentException e) { Â
            // Display message when exception occurs             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
// Java program to demonstrate checkedMap() method // of Collections class Â
// Importing all required classes import java.util.*; Â
// Main class public class GFG { Â
    // Main driver method     public static void main(String[] argv) throws Exception     { Â
        // Try block to check for exceptions         try { Â
            // Creating an empty HashMap by             // declaring object of string and integer type             HashMap<String, Integer> hmap                 = new HashMap<String, Integer>(); Â
            // Adding key-value pairs to above HashMap             // object             hmap.put( "Player-1" , 20 );             hmap.put( "Player-2" , 30 );             hmap.put( "Player-3" , 40 ); Â
            // Printing the elements inside above HashMap             // object             System.out.println( "Map: \n" + hmap); Â
            // Now creating typesafe view of the specified             // map using checkedMap() method             Map<String, Integer> tsmap                 = Collections.checkedMap(hmap, String. class ,                                          Integer. class ); Â
            // Again printing the typesafe view of specified             // list             System.out.println( "Typesafe view of Map: \n"                                + tsmap);         } Â
        // Catch block to handle exceptions         catch (IllegalArgumentException e) { Â
            // Display message when exception occurs             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}
Â