Java Concurrentmodificationexception Exception Analysis and Solutions

Source: Internet
Author: User
Tags concurrentmodificationexception

Java Concurrentmodificationexception Exception Analysis and Solutions
Http://www.2cto.com/kf/201403/286536.html

Java.util.ConcurrentModificationException Solutions
http://blog.csdn.net/lipei1220/article/details/9028669

Cause: Iterator do traversal, HashMap is modified (Bb.remove (ele), size-1), Iterator (Object ele=it.next ()) will check HashMap size,size change, Throws an error concurrentmodificationexception.

// 1 使用Iterator提供的remove方法,用于删除当前元素   for (Iterator<string> it = myList.iterator(); it.hasNext();) {       String value = it.next();        if (value.equals( "3" )) {            it.remove();  // ok       } } System. out.println( "List Value:" + myList.toString());  // 2 建一个集合,记录需要删除的元素,之后统一删除             List<string> templist = new ArrayList<string>();   for (String value : myList) {        if (value.equals( "3" )) {            templist.remove(value);       } }   // 可以查看removeAll源码,其中使用Iterator进行遍历 myList.removeAll(templist); System. out.println( "List Value:" + myList.toString());          // 3. 使用线程安全CopyOnWriteArrayList进行删除操作 List<string> myList = new CopyOnWriteArrayList<string>(); myList.add( "1" ); myList.add( "2" ); myList.add( "3" ); myList.add( "4" ); myList.add( "5" ); Iterator<string> it = myList.iterator();   while (it.hasNext()) {       String value = it.next();        if (value.equals( "3" )) {            myList.remove( "4" );            myList.add( "6" );            myList.add( "7" );       } } System. out.println( "List Value:" + myList.toString());  // 4. 不使用Iterator进行遍历,需要注意的是自己保证索引正常   for ( int i = 0 ; i < myList.size(); i++) {       String value = myList.get(i);       System. out.println( "List Value:" + value);        if (value.equals( "3" )) {            myList.remove(value);  // ok            i--; // 因为位置发生改变,所以必须修改i的位置       } } System. out.println( "List Value:" + myList.toString());

Java Concurrentmodificationexception Exception Analysis and Solutions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.