java.util.Collections.checkedMap() 方法
描述
checkedMap(Map<, V>, Class<K>, Class<V>) 方法用于获取指定映射的动态类型安全视图。
声明
以下是 java.util.Collections.checkedMap() 方法的声明。
public static <K,V> Map<K,V> checkedMap(Map<K,V> m,Class<K> keyType,Class<V> valueType)
参数
m − 这是要为其返回动态类型安全视图的映射。
keyType − 这是允许 m 持有的密钥类型。
valueType − 这是允许 m 持有的值的类型。
返回值
方法调用返回指定映射的动态类型安全视图。
异常
NA
示例
下面的例子展示了 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);
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Dynamically typesafe view of the map: {3=tutorials, 2=follow, 1=Always, 4=point}

