[英]java.util.ConcurrentModificationException while accessing hashmap
Why is the below exception happening? 为什么发生以下异常?
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) [:]
How have you tried to remove the object (key, value) in the map? 你是如何尝试删除地图中的对象(键,值)的? If you used the for-each Construct and tried to remove it the Exception will be thrown even if your code executes in a single-threaded environment.
如果您使用for-each Construct并尝试删除它,即使您的代码在单线程环境中执行,也会抛出Exception。
If you have iterated it like this: 如果你像这样迭代它:
for(Entry<String, Object> entry : session.entrySet()) {
if (condition) {
// throws a ConcurrentModificationException
session.remove(entry.getKey());
}
}
Then you should change it to that: 然后你应该改为:
Iterator<Entry<String, Object>> it = session.entrySet().iteration;
while (it.hasNext) {
Entry<String, Object> entry = it.next();
if (condition) {
it.remove(); // avoids a ConcurrentModificationException
}
}
Similar discussed in the question Iterate through a HashMap 类似于在问题中讨论通过HashMap迭代
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.