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

时间:2012-03-12 10:16:40

标签: java multithreading exception collections 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);

    }
}

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

2 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:2)

您是否尝试过使用Synchronized Hashmap? Collections.synchronizedMap(new HashMap())或查看ConcurrentHashMap

相关问题