[英]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.变量
tempOutfits
和outfits
都指向同一个实例。
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.