Original address: Https://www.jianshu.com/p/f3f6b12330c1 understanding and resolving Java Concurrency modification exceptions concurrentmodificationexception
I do not know if the reader in the Java development process encountered similar exception information Exception in thread "main" java.util.ConcurrentModificationException, The following is a brief description of the cause of the anomaly and the improvement of this anomaly, the content is very simple, there are any problems still look.
Suppose we want to implement an example of whether there is a "world" element in the set, and if so, add a "Java ee" element
The code for the exception appears as follows:
Import java.util.ArrayList;Import Java.util.Iterator;PublicClassTest {public static void main (string[] args) {arraylist<string> Array = new arraylist<string> (); //Create and add elements array.add ( "hello"); Span class= "hljs-built_in" >array.add ( "world"); array.add ( "Java"); Iterator it = array.iterator (); while (It.hasnext ()) {String s = (string) it.next (); if ( "World". Equals (s)) { "java ee");}} }}
1. Exception explanation
- Concurrentmodificationexception: This exception is thrown when the method detects concurrent modifications to an object, but does not allow this modification.
- Causes of the production:
Iterators are dependent on the collection and exist, after the success of the judgment, the collection of new elements added, and the iterator does not know, so the error, this error is called concurrency modification exception.
The simple description is that when an iterator iterates through an element, the element cannot be modified by the collection.
- How to solve it?
A: Iterator iteration element, iterator modification element
B: Set traversal element, set modify element (normal for)
2. Here are two ways to resolve this exception
Import java.util.ArrayList;PublicClassTest {PublicStaticvoidMain(string[] args) {arraylist<string>Array =New Arraylist<string> ();Creating and adding elementsArray.add ("Hello");Array.add ("World");Array.add ("Java");Method 1: Iterator iteration element, iterator modification elementThe iterator iterator does not add functionality, so we use its sub-interface ListiteratorListiterator lit = Array.listiterator ();while (Lit.hasnext ()) {string s = (string) lit.next ();if ("World". Equals (s)) {Lit.add ("Java ee");// }// }System.out.println ("List1:" + array);Method 2: Set traversal element, set modify element (normal for)for (int x = 0; x < array.size (); X + +) {string s = (string) array.get (x); if ( "World". Equals (s)) { "java ee");}} System.out.println ( "List2:" + array); //mode 3: Enhanced for loop //enhanced for Loop writes the same error because it is itself a substitute for the iterator's //for (String s:array) {//if ("World". Equals (s)) {//array.add (" Java ee "); //} //} //System.out.println ("LIST3:" + Array);}}
The gift rose, the hand is fragrant.
Understanding and resolving Java Concurrency modification Exceptions concurrentmodificationexception (reprint)