java.util.Collections.checkedMap() Method

Advertisements


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

Example

The following example shows the usage of java.util.Collections.checkedMap()

package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {
      // create map     
      HashMap<String,String> hmap = new HashMap<String,String>();
      
      // populate the map
      hmap.put("1", "Always");
      hmap.put("2", "follow");
      hmap.put("3", "tutorials");
      hmap.put("4", "point");
      
      // 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);
   }    
}

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

Dynamically typesafe view of the map: {3=tutorials, 2=follow, 1=Always, 4=point}



Advertisements
Advertisements