Solution to java. util. ConcurrentModificationException

Source: Internet
Author: User


This exception usually occurs when we traverse and delete a set element. Write the following code to demonstrate this exception.

[Java]
Import java. util. ArrayList;
Import java. util. List;

Public class ExeptionTest {
Public static void main (String [] args ){
List list = new ArrayList ();
List. add ("");
List. add ("B ");
List. add ("c ");
List. add ("d ");
List. add ("e ");

For (String s: list ){
If ("c". equals (s )){
List. remove ("c ");
}
}
}
}
[/Java]

The console reports the following error:

[Java]
Exception in thread "main" java. util. ConcurrentModificationException
At java. util. ArrayList $ Itr. checkForComodification (ArrayList. java: 819)
At java. util. ArrayList $ Itr. next (ArrayList. java: 791)
At ExeptionTest. main (ExeptionTest. java: 14)
[/Java]

Cause analysis:
During the for loop execution, the List is actually called to implement the Iterator interface method. In other words, all Iterator interfaces can be used. Trace the JDK source code and you can see that the error message is from the checkForComodification () method of the Itr iterator implemented in ArrayList.

[Java]
Final void checkForComodification (){
If (modCount! = ExpectedModCount)
Throw new ConcurrentModificationException ();
}
[/Java]

In fact, this method only does one thing: Check whether the current size of the iterator is the same as the original size, if not. This exception occurs because the original set has been modified elsewhere.

Solution
Since the cause of the error is clear, it is okay if we do not mix the two traversal methods.

[Java]
Import java. util. ArrayList;
Import java. util. Iterator;
Import java. util. List;

Public class ExeptionTest {
Public static void main (String [] args ){
List list = new ArrayList ();
List. add ("");
List. add ("B ");
List. add ("c ");
List. add ("d ");
List. add ("e ");

// Method 1
For (int I = 0; I <list. size (); I ++ ){
If (I = 2 ){
List. remove (I );
}
}
System. out. println (list );

// Method 2
Iterator iter = list. iterator ();
While (iter. hasNext ()){
If ("d". equals (iter. next ())){
Iter. remove ();
}
}
System. out. println (list );
}
}

[/Java]

ConcurrentModificationException advanced
In some cases, we may have to traverse and delete the time. How can this problem be solved? There are two solutions to this situation.

The objects to be deleted are collected to another collection to delete [java] import java. util. ArrayList;
Import java. util. Iterator;
Import java. util. List;
Public class ExeptionTest {
Public static void main (String [] args ){
List list = new ArrayList ();
List. add ("");
List. add ("B ");
List. add ("c ");
List. add ("d ");
List. add ("e ");

Iterator iter = list. iterator ();
List toBeRemove = new ArrayList ();
String t = null;
While (iter. hasNext ()){
T = iter. next ();
If (t. equals ("c ")){
ToBeRemove. add (t );
}
}
// Delete with removeAll
List. removeAll (toBeRemove );
System. out. println (list );

}
} [/Java]

The first round uses the copy object. [Java] import java. util. ArrayList;
Import java. util. List;
Public class ExeptionTest {
Public static void main (String [] args ){
List list = new ArrayList ();
List. add ("");
List. add ("B ");
List. add ("c ");
List. add ("d ");
List. add ("e ");
// Traverse the replication set. In this case, iterator is used.
For (String s: new ArrayList (list )){
If (s. equals ("c "))
// The non-iterator method is used for removal.
List. remove (s );
}
System. out. println (list );
}
}
[/Java]

This article is written here. If you have any omissions, please correct me.

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.