[英]Concurrent Modification exception when only values of Hashmap are modified
I have code like below, in which the inner loop modifies the Hashmap, but only in a way that no new keys are added or deleted, but only the values are updated. 我有下面的代码,其中的内部循环修改了Hashmap,但是仅以没有添加或删除新键的方式,而是仅更新了值。 Does this qualifies as modification of a Hashmap, for Concurrent Modification Exception to be thrown ?
这是否有资格作为Hashmap的修改,从而引发并发修改异常? In current tests that I have done, I haven't found any exception to be thrown though.
在当前完成的测试中,我没有发现任何异常。
for(String variable:variableMap.descendingKeySet()) {
for (String innerVariable : variableMap.keySet()) {
variableMap.put(innerVariable, variableMap.get(innerVariable).replace("$" + variable, variableMap.get(variable)));
}
}
See the Javadoc of HashMap
: 参见
HashMap
的Javadoc :
The iterators returned by all of this class's "collection view methods" are fail-fast : if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own
remove
method, the iterator will throw aConcurrentModificationException
.此类的所有“集合视图方法”返回的迭代器都是快速失败的 :如果在创建迭代器后的任何时间以任何方式对地图进行结构修改 ,则除了通过迭代器自己的
remove
方法之外,迭代器都会抛出ConcurrentModificationException
。
Now what is "structurally modified" ? 现在什么是“结构性修改”?
A structural modification is any operation that adds or deletes one or more mappings;
结构修改是添加或删除一个或多个映射的任何操作。 merely changing the value associated with a key that an instance already contains is not a structural modification.
仅更改与实例已经包含的键相关联的值 并不是结构上的修改。
So, no, you won't get a ConcurrentModificationException
if you only modify the value of the key. 因此,不,如果仅修改键的值,则不会获得
ConcurrentModificationException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.