Collections checkedMap() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Map: 
{Karn=Veer, Karan=Arjun, duryodhan=dhrupat, Ram=Shyam}
Typesafe view of Map: 
{Karn=Veer, Karan=Arjun, duryodhan=dhrupat, Ram=Shyam}

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Map: 
{Player-1=20, Player-3=40, Player-2=30}
Typesafe view of Map: 
{Player-1=20, Player-3=40, Player-2=30}


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.