访问hashmap时的java.util.ConcurrentModificationException
为什么会发生以下异常访问hashmap时的java.util.ConcurrentModificationException,java,exception,collections,hashmap,Java,Exception,Collections,Hashmap,为什么会发生以下异常 2012-08-28 11:41:59,183 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/TFO].[tfo]] (http-0.0.0.0-8080-9) Servlet.service() for servlet tfo threw exception: java.util.ConcurrentModificationException at j
2012-08-28 11:41:59,183 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/TFO].[tfo]] (http-0.0.0.0-8080-9) Servlet.service() for servlet tfo threw exception: java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793) [:1.6.0_24]
at java.util.HashMap$EntryIterator.next(HashMap.java:834) [:1.6.0_24]
at java.util.HashMap$EntryIterator.next(HashMap.java:832) [:1.6.0_24]
at net.sf.json.JSONObject._fromMap(JSONObject.java:1082) [:]
at net.sf.json.JSONObject.fromObject(JSONObject.java:173) [:]
at net.sf.json.JSONObject._processValue(JSONObject.java:2552) [:]
您是如何尝试删除地图中的对象(键、值)的?如果使用并试图删除,即使代码在单线程环境中执行,也会引发异常 如果您已按如下方式进行迭代:
for(Entry<String, Object> entry : session.entrySet()) {
if (condition) {
// throws a ConcurrentModificationException
session.remove(entry.getKey());
}
}
for(条目:session.entrySet()){
如果(条件){
//抛出ConcurrentModificationException
删除(entry.getKey());
}
}
那么您应该将其更改为:
Iterator<Entry<String, Object>> it = session.entrySet().iteration;
while (it.hasNext) {
Entry<String, Object> entry = it.next();
if (condition) {
it.remove(); // avoids a ConcurrentModificationException
}
}
Iterator it=session.entrySet().iteration;
while(it.hasNext){
Entry=it.next();
如果(条件){
it.remove();//避免ConcurrentModificationException
}
}
问题中讨论的类似问题如果不知道您试图做什么,则很难判断。可能的重复我刚刚从会话访问了map并再次删除了一个对象,我通过下面的链接将其设置为会话。。