[英]unmodifiable Map still accept adding entries without return UnsupportedOperationException
Am trying out adding entry to unmodifiable Map, JVM catch UnsupportedOperationException when adding entry to a subview of original Map, but JVM does not care about direct adding to the original view, code speaks : 我正在尝试将条目添加到不可修改的Map,将条目添加到原始Map的子视图时,JVM捕获了UnsupportedOperationException,但是JVM并不关心直接添加到原始视图中,代码表明:
ConcurrentMap<String, Integer> origView= new ConcurrentHashMap<String, Integer>();
Map<String,Integer> subView = Collections.unmodifiableMap(origView);
origView.put("s", 44); // ok
subView.put("p", 77); // java.lang.UnsupportedOperationException
However Documentation of Collections.unmodifiableSortedMap() says : 但是Collections.unmodifiableSortedMap()的文档说:
Attempts to modify the returned sorted map, whether direct, via its collection views, or via its subMap, headMap, or tailMap views, result in an UnsupportedOperationException.
尝试通过其集合视图或通过其subMap,headMap或tailMap视图直接修改返回的已排序地图会导致UnsupportedOperationException。
please lend hand, thanks . 请伸出援助之手,谢谢。
"Attempts to modify the returned sorted map. ..."
“尝试修改返回的排序图。...”
It will only throw an UnsupportedOperationException
on the Map
returned from the unmodifiableMap(...)
call, which does not include the original Map
. 它只会在
unmodifiableMap(...)
调用返回的Map
上抛出UnsupportedOperationException
,其中不包括原始Map
。
The Map
that gets returned from unmodifiableMap(...)
is not the same as the original Map
. 从
unmodifiableMap(...)
返回的Map
与原始Map
不相同。
Collections.unmodifiableMap()
returns an unmodifiable view of the original map; Collections.unmodifiableMap()
返回原始地图的不可修改视图; it does not change the original map in any way . 它不会以任何方式更改原始地图 。 That means that the original map is still modifiable.
这意味着原始地图仍然可以修改。 You will only get an exception if you try to modify the unmodifiable view.
如果您尝试修改不可修改的视图,则只会获得异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.