gpt4 book ai didi

java - 如何在避免 ConcurrentModificationException 的同时遍历 HashMap

转载 作者:行者123 更新时间:2023-11-29 10:05:25 31 4
gpt4 key购买 nike

我有一个 HashMap,它的类型是 HashMap<String,HashMap<String,int>>现在我需要遍历此 HashMap 并删除任何键值为 0 的内部 HashMap。

如果这样的删除使得内部 HashMap 为空,那么内部 HashMap 的相应键将从外部 HashMap 中删除。我尝试对其进行迭代,然后删除符合要求的元素,但这会给我一个 ConcurrentModificationException .

我尝试了以下代码:

synchronized(MyConstants.cliListUpdateList)
{
synchronized(MyConstants.cliList)
{
outerEntries = MyConstants.cliListUpdateList.entrySet();
outerIterator = outerEntries.iterator();

while(outerIterator.hasNext())
{
outerEnt = (Entry) outerIterator.next();
innerHashMap = (HashMap) outerEnt.getValue();
synchronized(innerHashMap)
{//synchronize innerhashmap
innerEntries = innerHashMap.entrySet();
innerIterator = innerEntries.iterator();
synchronized(innerIterator)
{
while(innerIterator.hasNext())
{
innerEnt = (Entry) innerIterator.next();
int k = Integer.parseInt((String)innerEnt.getValue());
if(k==0)
{
innerHashMap.remove(innerEnt.getKey());
if(innerHashMap.isEmpty())
{
MyConstants.cliListUpdateList.remove(outerEnt.getKey());
}

ArrayList ports = (ArrayList) MyConstants.cliList.get(outerEnt.getKey());
ports.remove((String)innerEnt.getKey());
if(ports.isEmpty())
{
MyConstants.cliList.remove(outerEnt.getKey());
}
}
else
{
k--;
innerHashMap.put(innerEnt.getKey(), k+"");
MyConstants.cliListUpdateList.put(outerEnt.getKey(), innerHashMap);
}

}
}
}//synchronize innerhashmap
}


System.out.println(MyConstants.cliListUpdateList + " <---> "+ MyConstants.cliList);

}
}

我在这一行遇到异常:innerEnt = (Entry) innerIterator.next(); .我尝试了 Iterator 类提供的 remove 方法。但这也不好。

编辑

从 Java 文档我知道这么多 if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this(ConcurrentModificationException) exception但我需要完全相同的功能。

最佳答案

可能无法完全解决您的问题,但您需要使用迭代器的移除方法 innerIterator.remove(); 而不是 innerHashMap.remove(innerEnt.getKey()); >

关于java - 如何在避免 ConcurrentModificationException 的同时遍历 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9665215/

31 4 0
文章推荐: python - 表名 SQL 语法错误
文章推荐: Java 按位非
文章推荐: java - 不可预测的双重
文章推荐: java - 了解 float 计数器
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com