Concurrentmodificationexception: Concurrency Modification exception

Source: Internet
Author: User

Requirements: I have a collection, such as the next, I would like to judge there is no "world" this element, if there is, I will add a "Java ee" element, please write code implementation.

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

public class Demo {
public static void Main (string[] args) {

list<string> list = new arraylist<string> ();

List.add ("Hello");
List.add ("World");
List.add ("Java");

Iterator<string> it = List.iterator ();
while (It.hasnext ()) {
String s = it.next ();
if ("World". Equals (s)) {
List.add ("Java ee");
}
}

System.out.println ("list:" + list);
}
}

The above code will appear concurrentmodificationexception at run time: Concurrency modification exception

Causes of the production:

Iterators are dependencies and collections exist, after the success of the decision, the collection center added elements, and iterators do not know, so the error, which is called concurrent modification exception.

What this problem describes is that when an iterator iterates through an element, the element cannot be modified by a collection.

So what should you do when you encounter such an exception?

Way one: We can use iterators to iterate over elements, and iterators to add elements.

At this point we are using an iterator that is unique to the list collection listiterator the Listiterator () list iterator.

The iterator inherits the iterator iterator, so you can use the Hasnext () and Next () methods directly.

Additionally, the Listiterator void Add (Object obj) method can add elements.

Note: The elements added using the list iterator Listiterator are followed by the elements that were just iterated.

Then the code that uses the list iterator to complete the above requirements is reflected in the following:

Import java.util.ArrayList;
Import java.util.List;
Import Java.util.ListIterator;

public class Demo {
public static void Main (string[] args) {

list<string> list = new arraylist<string> ();

List.add ("Hello");
List.add ("World");
List.add ("Java");

Listiterator<string> lit = List.listiterator ();
while (Lit.hasnext ()) {
String s = lit.next ();
if ("World". Equals (s)) {
Lit.add ("Java ee");
}
}

System.out.println ("list:" + list);
}
}

Way two: Use the set to traverse elements, set to modify elements (using normal for)

Use the size () and get () methods of the list collection to iterate through the collection

Note that using normal for traversal and modify collection elements, the elements are all added at the last

Then the code that uses the normal for to complete the above requirements is reflected as:

Import java.util.ArrayList;
Import java.util.List;

public class Demo {
public static void Main (string[] args) {

list<string> list = new arraylist<string> ();

List.add ("Hello");
List.add ("World");
List.add ("Java");

   

for (int x = 0; x < list.size (); + +) {
String s = list.get (x);
if ("World". Equals (s)) {
List.add ("Java ee");
}
}

System.out.println ("list:" + list);
}
}

Concurrentmodificationexception: Concurrency Modification exception

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.