import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { String codes = "HH|140402001>RQSPF=1+SPF=3,140402002>RQSPF=3+SPF=1|2*1"; Pattern p = Pattern.compile("(\\d{9})"); Matcher m = p.matcher(codes); Map<String, String> rqMap = new HashMap<String, String>(); List<String> itemList = new ArrayList<String>(); CopyOnWriteArrayList<String> currList = new CopyOnWriteArrayList<String>(itemList); // List<String> currList = Collections.synchronizedList(itemList); //报ConcurrentModificationException异常 while (m.find()) { // System.out.println(m.group()); itemList.add(matcher.group()); } for (String yhItem : currList) { //正确 String totalItem = xml.getStringValue("row[" + i + "].@close"); if (yhItem.equals(mid)) { rqMap.put(yhItem, totalItem); currList.remove(yhItem); } } // for (int j=0; j<itemList.size(); j++) { //正确 //或者使用 list.iterator 的remove() 或者使用CopyOnWriteArrayList,使用其它的方式可能会报ConcurrentModificationException异常 // String totalItem = xml.getStringValue("row[" + i + "].@close"); // String yhItem = itemList.get(j); // if (yhItem.equals(mid)) { // rqMap.put(yhItem, totalItem); // itemList.remove(j); // } // } // Iterator<String> iterator = itemList.iterator(); //正确 // while (iterator.hasNext()) { // String totalItem = xml.getStringValue("row[" + i + "].@close"); // String yhItem = iterator.next(); // if (yhItem.equals(mid)) { // rqMap.put(yhItem, totalItem); // iterator.remove(); // } // } // for (String yhItem : itemList) { //报ConcurrentModificationException异常 // String totalItem = xml.getStringValue("row[" + i + "].@close"); // if (yhItem.equals(mid)) { // rqMap.put(yhItem, totalItem); // int k = itemList.indexOf(yhItem); // itemList.remove(k); // } // } } }
详细源代码分析及原理摘自其它的博客:
http://arron-li.iteye.com/blog/645008
http://blog.csdn.net/aa4790139/article/details/6438869
http://www.cnblogs.com/yanghuahui/p/3365509.html
相关推荐
java.util.ConcurrentModificationException 解决方法 在使用iterator.hasNext()操作迭代器的时候,如果此时迭代的对象发生改变,比如插入了新数据,或者有数据被删除。 则使用会报以下异常: Java.util....
java.util.ConcurrentModificationException 异常问题详解1
主要介绍了出现java.util.ConcurrentModificationException 问题及解决办法的相关资料,需要的朋友可以参考下
今天小编就为大家分享一篇关于Java源码解析ArrayList及ConcurrentModificationException,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
NULL 博文链接:https://chenlinbo.iteye.com/blog/832335
axis1.4补丁包,解决jdk1.8高并发报ConcurrentModificationException问题,该jar包重新编译jar包的一个class文件,线上环境通过
Iterator遍历中 ConcurrentModificationException异常
axis1.4 spring3.0 集成 实现 web service 服务端, axis1.4 客户端认证,授权,访问日志记录,集成spring 解决 PHP 调用web service 无法认证,和解析soap 模板
Map在遍历时候通常 现获得其键值的集合Set,然后用迭代器Iterator来对Map进行遍历。
fastJson的全部资料,包括源码、开发需要用到的jar包和html格式的文档。
这里面包含了大部分的软件测试的专业术语,希望对你有用
Spring数据mongodb测试 在Collections.synchronizedList或Collections.synchronizedSet上测试spring数据mongodb ConcurrentModificationException
iterator和listIterator方法是快速失败的 :如果列表在任何时间从结构上修改创建迭代器之后,以任何方式除非通过迭代器自身remove种或add方法,迭代器都将抛出一个ConcurrentModificationException 。 因此,面对...
继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。对象的一个新类可以从现有的类中派生,这个过程称为类继承。新类继承了原始类的特性,新类称为原始类的派生类(子类),而...
基本涵盖Axis 1.4 开发所需要的jar包
除了实现List接口,此类提供方法来操作在内部用于存储列表中的阵列的大小。 (这个类是大致相当于矢量 ,除了它是不同步的)。 大小 的isEmpty, 获取 , 设置 迭代器和操作的ListIterator在固定时间内运行。 在...
java.util.ConcurrentModificationException: mutation occurred during iteration [error] scala.collection.mutable.MutationTracker$.checkMutations(MutationTracker.scala:43) [error] scala.collection....
76.EJB有哪几种?区别是什么? 77.JavaBean与EJB有什么区别? 78.软件开发生命周期有哪几个阶段? 79.软件开发有哪些因素? 80.软件开发中如何进行版本控制? 81.UML中,类视图如何表示类中的继承与聚合? 82.客户端...