【发布时间】:2015-07-30 21:30:38
【问题描述】:
我有 2 个哈希图:HashMap<String, Word> hmSmall, hmBig。每个中的映射都是多对一。因此,基于某些标准,我必须删除与两个哈希图中的特定值相对应的所有映射。为此,我正在关注this 的回答。
问题是我在遍历哈希图时仍然得到ConcurrentModificationException。
代码如下:
for (Iterator<Word> it = hmSmall.values().iterator(); it.hasNext(); i++) {
Word value = it.next();//java.util.ConcurrentModificationException here
String key = value.getKey();
if (hmBig.containsKey(key)) {
// if big contains less then remove from big
// else it.remove
if (hmBig.get(key).getTotalOccurances() < value
.getTotalOccurances()) {
hmBig.values().removeAll(
Collections.singleton(hmBig.get(key)));
} else {
// it.remove(); not using this based on https://stackoverflow.com/a/30214164/848377
hmSmall.values().removeAll(Collections.singleton(value));
}
}
}
有简单的出路吗?
【问题讨论】:
标签: java