如何在避免ConcurrentModificationException的同时迭代HashMap

sas*_*har 1 java collections multithreading exception concurrentmodification

我有一个HashMap,它的类型HashMap<String,HashMap<String,int>>现在我需要迭代这个HashMap并删除任何键值为0的内部HashMaps.

如果这样的删除使内部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);

    }
}
Run Code Online (Sandbox Code Playgroud)

我在这一行得到了例外: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但我需要完全相同的功能.

Old*_*eon 7

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