这个问题已经在这里有一个答案:
- 迭代整个集合,避免ConcurrentModificationException的在一个循环中删除对象时 23个回答
我有一个方法试验(+),其中我试图来比较两个互相LinkedHashMaps和如果在这两个LHM的发现删除键/值对修改地图的内容之一。 我一直运行此方法时得到一个ConcurrentModificationException的。 我明白为什么我得到异常(因为我试图修改被架设到列表)。 我不知道如何与这个可是出去。 我有这样的代码至今:
private void test() {
LinkedHashMap<String, BigDecimal>testBene = new LinkedHashMap<String, BigDecimal>();
LinkedHashMap<String, BigDecimal>testDly = new LinkedHashMap<String, BigDecimal>();
testBene.put("ABCDEFG", BigDecimal.ZERO);
testBene.put("BCDEFGH", BigDecimal.ONE);
testBene.put("CDEFGHI", BigDecimal.TEN);
testDly.put("BCDEFGH", BigDecimal.ONE);
testDly.put("Foo", BigDecimal.TEN);
testDly.put("Bar", BigDecimal.TEN);
for (Entry<String, BigDecimal> beneKeySet : testBene.entrySet()) {
if (testDly.containsKey(beneKeySet.getKey())) {
for (Entry<String, BigDecimal> dlyKeySet : testDly.entrySet()) {
if ((dlyKeySet.getKey().equals(beneKeySet.getKey())) &&
dlyKeySet.getValue().equals(beneKeySet.getValue())) {
testBene.remove(dlyKeySet.getKey());
}
}
}
}
}