我声明了ImmutableMap之类的
public static ImmutableMap<String,String> mapImmutable;
为该变量分配一个映射
mapImmutable= ImmutableMap.copyOf(map2);
现在,如果我将其他地图分配给此“ mapImmutable”变量。它不会引发任何异常,并且会更新值。
mapImmutable=ImmutableMap.copyOf(map3);
公共类UnmodifiedMap {
public static ImmutableMap<String,String> mapImmutable;
public static void main(String[] args) {
Map<String,String> map2=new HashMap<String,String>();
map2.put("name","mark");
mapImmutable= ImmutableMap.copyOf(map2);
System.out.println(mapImmutable);
Map<String,String> map3=new HashMap<String,String>();
map3.put("Country","USA");
map3.put("name","Joy");
mapImmutable=ImmutableMap.copyOf(map3);\\It should throw an exception while reassign.
System.out.println(mapImmutable);}}
控制台结果-: {name = mark} {Country = USA}
重新分配时应该抛出异常。