java.util.ConcurrentModificationException 异常
2016-11-23 11:01
519 查看
这个异常是我们在迭代遍历集合的时候,使用iterator 来进行遍历。遍历过程中你对迭代的对象进行了删除或者添加操作。造成了这个异常。
当然也包含for循环。
在每次遍历的时候,iterator都会去检查size 如果发生改变就抛出异常。可以看java源码。
解决办法:
1.让iterator去修改而不是你去修改。在循环中使用iterator.remove();
2.使用“ConcurrentHashMap”替换HashMap,ConcurrentHashMap会自己检查修改操作,对其加锁,也可针对插入操作
如果我们要进行删除,一般采用
Map<String,String> map = new HashMap<String,String>();
for(Iterator it=map.keySet().iterator();it.hasNext();){
Map.Entry<String,String> entry = (Map.Entry<String,String> )it.next();
it.remove();
}
这种迭代不支持添加。
如果真的要添加,你可以再写一个Map然后push进去,当然 删除也是同理。。或者你遍历这个,然后删除你new的。
此处fields 是有数据需要遍历的对象。
Map<String, Map<String, Object>> result = new HashMap<String, Map<String, Object>>();
result.putAll(fields);
for (Iterator it = fields.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Map<String, Object>> entry = (Entry<String, Map<String, Object>>) it.next();
String key = entry.getKey();
if (key.startsWith("faEmp")) {
result.put(key.replace("faEmp:", "faEmp:o."), entry.getValue());
result.remove(key);
} else if (key.startsWith("faUser")) {
result.put(key.replace("faUser:", "faUser:u."), entry.getValue());
result.remove(key);
} else if (key.startsWith("employee")) {
result.put(key.replace("employee:", "employee:e."), entry.getValue());
result.remove(key);
}
}
fields.putAll(result);
当然也包含for循环。
在每次遍历的时候,iterator都会去检查size 如果发生改变就抛出异常。可以看java源码。
解决办法:
1.让iterator去修改而不是你去修改。在循环中使用iterator.remove();
2.使用“ConcurrentHashMap”替换HashMap,ConcurrentHashMap会自己检查修改操作,对其加锁,也可针对插入操作
如果我们要进行删除,一般采用
Map<String,String> map = new HashMap<String,String>();
for(Iterator it=map.keySet().iterator();it.hasNext();){
Map.Entry<String,String> entry = (Map.Entry<String,String> )it.next();
it.remove();
}
这种迭代不支持添加。
如果真的要添加,你可以再写一个Map然后push进去,当然 删除也是同理。。或者你遍历这个,然后删除你new的。
此处fields 是有数据需要遍历的对象。
Map<String, Map<String, Object>> result = new HashMap<String, Map<String, Object>>();
result.putAll(fields);
for (Iterator it = fields.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Map<String, Object>> entry = (Entry<String, Map<String, Object>>) it.next();
String key = entry.getKey();
if (key.startsWith("faEmp")) {
result.put(key.replace("faEmp:", "faEmp:o."), entry.getValue());
result.remove(key);
} else if (key.startsWith("faUser")) {
result.put(key.replace("faUser:", "faUser:u."), entry.getValue());
result.remove(key);
} else if (key.startsWith("employee")) {
result.put(key.replace("employee:", "employee:e."), entry.getValue());
result.remove(key);
}
}
fields.putAll(result);
相关文章推荐
- 从ArrayList中删除对象报java.util.ConcurrentModificationException异常解决
- java迭代器异常 java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry
- java.util.ConcurrentModificationException异常 解决方法
- java.util.ConcurrentModificationException异常
- 异常:java.util.ConcurrentModificationException
- java.util.ConcurrentModificationException异常的解决办法
- Java中的java.util.ConcurrentModificationException异常
- java.util.ConcurrentModificationException异常
- 主题:java.util.ConcurrentModificationException 异常解决办法
- 关于出现List的java.util.ConcurrentModificationException异常问题的解决
- java.util.ConcurrentModificationException异常处理
- java.util.ConcurrentModificationException异常解决
- java.util.ConcurrentModificationException异常解决方法
- java.util.ConcurrentModificationException异常
- 为什么会出现 java.util.ConcurrentModificationException 异常?
- 为什么会出现 java.util.ConcurrentModificationException 异常?
- 为什么会出现 java.util.ConcurrentModificationException 异常?
- 【转】java.util.ConcurrentModificationException异常原因
- java.util.ConcurrentModificationException异常
- 为什么会出现 java.util.ConcurrentModificationException 异常?