ConcurrentModificationException Woes [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 09:04:49

You could use an iterator:

for (Iterator<Entry<String, BigDecimal>> it = testBene.entrySet().iterator(); it.hasNext();) {
    Entry<String, BigDecimal> beneKeySet = it.next();
    if (testDly.containsKey(beneKeySet.getKey())) {
        for (Entry<String, BigDecimal> dlyKeySet : testDly.entrySet()) {
            if ((dlyKeySet.getKey() == beneKeySet.getKey()) && dlyKeySet.getValue() == beneKeySet.getValue()) {
                it.remove();
            }
        }
    }
}

Instead of removing the elements, put the keys that you want to remove into a separate collection. At the end, traverse that other collection, removing the keys from your map.

Alternatively, use the Iterator interface instead of the for-each loop. This will enable you to use Iterator.remove() to remove elements while iterating.

You cannot remove from a list you're currently iterating with a for each. Use the list's iterator to do this.

You can use EntrySet's iterator, or save all duplicated keys in another list, and later remove those from the map. Also, do not compare objects using ==, use the equals() function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!