简体   繁体   English

访问hashmap时出现java.util.ConcurrentModificationException

[英]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.

相关问题 HashMap java.util.ConcurrentModificationException - HashMap java.util.ConcurrentModificationException java.util.ConcurrentModificationException:对哈希图的并发访问 - java.util.ConcurrentModificationException: concurrent access to hashmap 使用迭代器时出现java.util.ConcurrentModificationException - java.util.ConcurrentModificationException while using iterator java.util.ConcurrentModificationException 同时改变对象 - java.util.ConcurrentModificationException while mutating an object 使用 ExecutorService 时出现 java.util.ConcurrentModificationException - java.util.ConcurrentModificationException while using ExecutorService 我在使用HashMap时抛出java.util.ConcurrentModificationException - I am getting java.util.ConcurrentModificationException thrown while using HashMap 知道为什么我在从 HashMap 中删除键时没有收到 java.util.ConcurrentModificationException 吗? - Any Idea why I am not getting java.util.ConcurrentModificationException while removing key from HashMap? 可以更改HashMap的值的成员导致java.util.ConcurrentModificationException - Can changing members of values of HashMap cause java.util.ConcurrentModificationException HashMap迭代/删除获取java.util.ConcurrentModificationException - HashMap iteration/removal getting java.util.ConcurrentModificationException 从HashMap中删除元素时发生异常java.util.ConcurrentModificationException - Exception when removing element from HashMap java.util.ConcurrentModificationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM