Java Collection Tutorial - Java Collections.checkedMap(Map <K,V> m, Class <K> keyType, Class <V> valueType)








Syntax

Collections.checkedMap(Map <K,V> m, Class <K> keyType, Class <V> valueType) has the following syntax.

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

Example

In the following code shows how to use Collections.checkedMap(Map <K,V> m, Class <K> keyType, Class <V> valueType) method.

/*  www .ja v  a2s  .  co m*/

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

public class Main {
   public static void main(String args[]) {
      
      HashMap<String,String>  hmap = new HashMap<String,String> ();
      
 
      hmap.put("1", "A");
      hmap.put("2", "B");
      hmap.put("3", "C");
      hmap.put("4", "from java2s.com");
      
      // get typesafe view of the map
      Map<String,String>  tsmap = Collections.checkedMap(hmap,String.class,String.class);     
      
      System.out.println(tsmap);
   }    
}

The code above generates the following result.