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:
// 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}
Recommended Posts:
- Collections max() method in Java with Examples
- Collections min() method in Java with Examples
- Collections replaceAll() method in Java with Examples
- Collections swap() method in Java with Examples
- Collections synchronizedCollection() method in Java with Examples
- Collections newSetFromMap() method in Java with Examples
- Collections synchronizedList() method in Java with Examples
- Collections copy() method in Java with Examples
- Collections fill() method in Java with Examples
- Collections indexOfSubList() method in Java with Examples
- Collections list() method in Java with Examples
- Collections synchronizedMap() method in Java with Examples
- Collections singletonList() method in Java with Examples
- Collections synchronizedSortedSet() method in Java with Examples
- Collections synchronizedSortedMap() method in Java with Examples
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.



