cerulean

ConcurrentModificationException

遍历集合,批量删除时,遇到ConcurrentModificationException
因为操作的集合会在过程中遇到结构性的改变,例如:
HashMap<String, String> testMap = new HashMap<String, String>();
        testMap.put(
"1""a");
        testMap.put(
"2""b");
        testMap.put(
"3""c");
        Set
<Map.Entry<String,String>> enterySet = testMap.entrySet();
        

        
for(Iterator<Map.Entry<String, String>> i = enterySet.iterator(); i.hasNext();){
            Map.Entry
<String, String> entry = i.next();
            
if(entry.getValue().equalsIgnoreCase("a")){
                testMap.remove(entry.getKey());
            }

        }


解决方法:不要在集合上删除,而是在迭代器上删除:i.remove();

p.s.还有另外一个可能性是多线程同时操作该集合了,那需要进行同步来避免。

posted on 2010-06-11 13:57 cerulean 阅读(347) 评论(0)  编辑  收藏 所属分类: Java


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2010年6月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

统计

常用链接

留言簿(3)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜