java.util.ConcurrentModificationException
来源:互联网 发布:sumts是什么网络类型 编辑:程序博客网 时间:2024/06/01 19:40
遍历List的时候不能删除元素。代码如下:
private final static Map<String, List<Map<String, String>>> newServerInfo = new HashMap<String, List<Map<String, String>>>();private final static Map<String, Long> newServerInfoDate = new HashMap<String, Long>(); for (Entry<String, Long> entry : newServerInfoDate.entrySet()) {String key = entry.getKey();long date = entry.getValue();System.out.println(System.currentTimeMillis() - date + "---"+ outTimeNum * 1000);if (System.currentTimeMillis() - date >= outTimeNum * 1000) { newServerInfo.remove(key); newServerInfoDate.remove(key); if (newServerInfoDate.size() == 0)break;}}for循环遍历时不可删除,修改如下:
List<String> removeKey = new ArrayList<String>();for (Entry<String, Long> entry : newServerInfoDate.entrySet()) {String key = entry.getKey();long date = entry.getValue();System.out.println(System.currentTimeMillis() - date + "---"+ outTimeNum * 1000);if (System.currentTimeMillis() - date >= outTimeNum * 1000) { removeKey.add(key); if (newServerInfoDate.size() == 0) break;}} for (String key : removeKey) {newServerInfo.remove(key);newServerInfoDate.remove(key);}遍历时,把需要删除的元素的key保存下来,遍历完成之后再删除。
0 0
- 【ConcurrentModificationException】java.util.ConcurrentModificationException 解决办法
- java.util.ConcurrentModificationException! java.util.ConcurrentModificationException!
- java: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
- java.util.ConcurrentModificationException
- java.util.ConcurrentModificationException
- java.util.ConcurrentModificationException
- java.util.ConcurrentModificationException
- java.util.ConcurrentModificationException 异常
- jquery获取屏幕高度
- mahout itemCF 简单使用
- Plus One
- 使用二维码生成框架libqrencode时遇到的问题解决办法
- Day1_Java基础
- java.util.ConcurrentModificationException
- Python基础教程(九):面向对象、正则表达式
- shareSDK使用分享
- 为什么程序员英文要好?(From:V2EX)
- 小知识汇总
- DIV 包括另外一个DIV 点击里面的DIV不能触发外面的DIV事件
- Java Memory Model
- 发现大量的TIME_WAIT解决办法
- Xcode错误:Undefined symbols for architecture x86_64