简体   繁体   English

java.util.ConcurrentModificationException,但我正在迭代一个副本,而不是我正在修改的集合

[英]java.util.ConcurrentModificationException, but I'm iterating over a copy, not the collection I'm modifying

Please read the description of my problem before marking duplicate.在标记重复之前,请阅读我的问题描述。

private void cleanTrash() {

    HashMap<String, OutfitInfo> tempOutfits = outfits;
    Iterator iter = tempOutfits.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry mapElement = (Map.Entry) iter.next();
        String key = (String) mapElement.getKey();
        OutfitInfo info = (OutfitInfo) mapElement.getValue();
        if (info.getParentID().equals(trashID)) {
            outfits.remove(key);
        }
    }
    HashMap<String, ItemInfo> tempItems = items;
    iter = tempItems.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry mapElement = (Map.Entry) iter.next();
        String key = (String) mapElement.getKey();
        ItemInfo info = (ItemInfo) mapElement.getValue();
        if (info.getParentID().equals(trashID)) {
            items.remove(key);
        }
    }

}

In the above code I am getting the concurrent modification exception.在上面的代码中,我得到了并发修改异常。 I understand that this error means that I can't modify a collection while I'm iterating over it.我了解此错误意味着我无法在迭代集合时对其进行修改。 HOWEVER , I made a copy of the collection before assigning the iterator, and the collection being modified is not the one that's being iterated over.但是,我在分配迭代器之前制作了集合的副本,并且正在修改的集合不是被迭代的集合。 So why am I still getting this message, and how do I fix it?那么为什么我仍然收到此消息,我该如何解决?

HashMap<String, OutfitInfo> tempOutfits = outfits; does not create a copy.不创建副本。 Both varaibles tempOutfits and outfits point to the same instance.变量tempOutfitsoutfits都指向同一个实例。

If you actually want to copy you can do:如果你真的想复制你可以这样做:

 HashMap<String, OutfitInfo> tempOutfits = new HashMap<>(outfits);

This line creates a new HashMap and initializes it with the content of outfits此行创建一个新的 HashMap 并使用outfits的内容对其进行初始化


As you already have the Iterator , instead of removing the entry directly from the collection.由于您已经拥有Iterator ,而不是直接从集合中删除条目。 Call iterator.remove();调用iterator.remove(); to remove the item the iterator currently references.删除迭代器当前引用的项目。 No need to copy the collection.无需复制集合。

So:所以:

private void cleanTrash() {
    Iterator iter = outfits.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry mapElement = (Map.Entry) iter.next();
        String key = (String) mapElement.getKey();
        OutfitInfo info = (OutfitInfo) mapElement.getValue();
        if (info.getParentID().equals(trashID)) {
            iter.remove();
        }
    }
    iter = items.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry mapElement = (Map.Entry) iter.next();
        String key = (String) mapElement.getKey();
        ItemInfo info = (ItemInfo) mapElement.getValue();
        if (info.getParentID().equals(trashID)) {
            iter.remove();
        }
    }
}

You can use clone() to create a copy of the HashMap, clone()#Oracle doc您可以使用clone()创建 HashMap 的副本, clone()#Oracle doc

Map<String, String> copy = (Map<String, String>) outfits.clone();

    public static void main(String[] args) {
        HashMap<String, String> outfits = new HashMap<>(Map.of("1", "11", "2", "22"));
        Map<String, String> copy = (Map<String, String>) outfits.clone();
        Iterator<Entry<String, String>> iterator = copy.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<String, String> next = iterator.next();
            if (next.getKey().equals("1"))
                outfits.remove(next.getKey());
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历排序的树图:java.util.ConcurrentModificationException - iterating over sorted treemap: java.util.ConcurrentModificationException java.util.ConcurrentModificationException但我没有删除 - java.util.ConcurrentModificationException But I am not removing 我有一个java.util.ConcurrentModificationException - I am having a java.util.ConcurrentModificationException 修改列表时出现java.util.ConcurrentModificationException - java.util.ConcurrentModificationException when modifying list 如何同步以防止java.util.ConcurrentModificationException - How do I synchronize to prevent a java.util.ConcurrentModificationException 为什么我得到java.util.ConcurrentModificationException? - Why am I getting java.util.ConcurrentModificationException? 为什么在这个例子中我没有得到 java.util.ConcurrentModificationException ? - Why am I not getting a java.util.ConcurrentModificationException in this example? 为什么使用迭代器获取java.util.ConcurrentModificationException? - Why am I getting java.util.ConcurrentModificationException with an iterator? 我在Java中收到“ concurrentModificationException”,但未更改要遍历的列表 - I'm getting a “concurrentModificationException” in java, but I'm not changing the list I'm iterating through 为什么我在高级for循环中收到java.util.ConcurrentModificationException? - why am i getting a java.util.ConcurrentModificationException in the advanced for loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM