Java Collections checkedMap() Method



Description

The checkedMap(Map<, V>, Class<K>, Class<V>) method is used to get a dynamically typesafe view of the specified map.

Declaration

Following is the declaration for java.util.Collections.checkedMap() method.

public static <K,V> Map<K,V> checkedMap(Map<K,V> m,Class<K> keyType,Class<V> valueType)

Parameters

  • m − This is the map for which a dynamically typesafe view is to be returned.

  • keyType − This is the type of key that m is permitted to hold.

  • valueType − This is the type of value that m is permitted to hold.

Return Value

The method call returns a dynamically typesafe view of the specified map.

Exception

NA

Getting a TypeSafe Map from a Map of String, Integer pair Example

The following example shows the usage of Java Collection checkedMap(Map,Class,Class ) method to get a typesafe view of map of String and Integers. We've created a Map object with some string and integers. Using checkedMap(Map, String, Integer) method, we're getting a map of String, Integer and then it is printed.

package com.tutorialspoint;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class CollectionsDemo {
   public static void main(String args[]) {
   
      // create map     
      HashMap<String,Integer> hmap = new HashMap<>();

      // populate the map
      hmap.put("1", 1);
      hmap.put("2", 2);
      hmap.put("3", 3);
      hmap.put("4", 4);

      // get typesafe view of the map
      Map<String,Integer> tsmap;
      tsmap = Collections.checkedMap(hmap,String.class,Integer.class);     

      System.out.println("Dynamically typesafe view of the map: "+tsmap);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Dynamically typesafe view of the map: {1=1, 2=2, 3=3, 4=4}

Getting a TypeSafe Map from a Map of String, String pair Example

The following example shows the usage of Java Collection checkedMap(Map,Class,Class ) method to get a typesafe view of map of String and Strings. We've created a Map object with some string and strings. Using checkedMap(Map, String, String) method, we're getting a map of String, String and then it is printed.

package com.tutorialspoint;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class CollectionsDemo {
   public static void main(String args[]) {
   
      // create map     
      HashMap<String,String> hmap = new HashMap<>();

      // populate the map
      hmap.put("1", "A");
      hmap.put("2", "B");
      hmap.put("3", "C");
      hmap.put("4", "D");

      // get typesafe view of the map
      Map<String,String> tsmap;
      tsmap = Collections.checkedMap(hmap,String.class,String.class);     

      System.out.println("Dynamically typesafe view of the map: "+tsmap);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Dynamically typesafe view of the map: {1=A, 2=B, 3=C, 4=D}

Getting a TypeSafe Map from a Map of String, Object pair Example

The following example shows the usage of Java Collection checkedCollection(Map,Class ) method to get a typesafe view of map of Student objects. We've created a map with some student objects, printed the original map. Using checkedCollection(Map, Student) method, we're getting a map of String and then it is printed.

package com.tutorialspoint;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class CollectionsDemo {

   public static void main(String[] args) {
   
      // create map     
      HashMap<String,Student> hmap = new HashMap<>();

      // populate the map
      hmap.put("1", new Student(1, "Julie"));
      hmap.put("2", new Student(2, "Robert"));
      hmap.put("3", new Student(3, "Adam"));
      hmap.put("4", new Student(4, "Jene"));

      // get typesafe view of the map
      Map<String,Student> tsmap;
      tsmap = Collections.checkedMap(hmap,String.class,Student.class);     

      System.out.println("Dynamically typesafe view of the map: "+tsmap);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result.

Dynamically typesafe view of the map: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ], 4=[ 4, Jene ]}
java_util_collections.htm
Advertisements