全屏
unmodifiableMap()方法用于返回指定映射的不可修改视图。
声明
以下是java.util.Collections.unmodifiableMap()方法的声明。public static Map unmodifiableMap(Map extends K,? extends V> m)
参数m--这是一个不可修改视图是要返回的映射。
返回值方法调用返回指定映射的不可修改视图。
异常NA
例子
下面的例子显示java.util.Collections.unmodifiableMap()方法的使用
package cn.sxt;
import java.util.*;
public class CollectionsDemo {
public static void main(String[] s) {
//object hash table
Hashtable table = new Hashtable();
// populate the table
table.put("key1", "value1");
table.put("key2", "value2");
table.put("key3", "value3");
System.out.println("Initial collection: "+table);
// create unmodifiable map
Map m = Collections.unmodifiableMap(table);
// try to modify the collection
m.put("key3", "value3");
}
}
现在编译和运行上面的代码示例,将产生以下结果。Initial collection: {key3=value3, key2=value2, key1=value1}
Exception in thread "main" java.lang.UnsupportedOperationException
分享到:
0评论