Java. util. concurrentmodificationexception

Source: Internet
Author: User
Tags concurrentmodificationexception

[This blog post, thanks to the help of the Administrator Gg of cnblogs, allows us to see the day again. Thank you!]

A cute little bug when using set/Map: Java. util. concurrentmodificationexception

 

[Error scenario 1]: Set container, edge traversal, edge Add/Remove Element

Set<String> set = new HashSet<String>();
for (int i = 0; i < 10000; i++) {
    set.add(Integer.toString(i));
}
For (string STR: Set) {// or use iterator for loop. For jdk5.0 or later, the underlying traversal is also implemented by iterator.
Set. Add ("XXX"); // Error
// Set. Remove (STR); // Error
}
 
[Error scenario 2]: Map container, edge traversal, and edge removal element
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < 100; i++) {
    map.put(Integer.toString(i), Integer.toString(i));
}
For (string STR: map. keyset () {// or use iterator to loop
Map. Remove (STR); // Error
}
 
[Error scenario 3] list container, edge traversal, edge Add/Remove Element
List<String> list = new ArrayList<String>();
        for (int i = 0; i < 100; i++) {
            list.add(Integer.toString(i));
        }
        for (Iterator<String> it = list.iterator(); it.hasNext();) { 
            String val = it.next();
            if (val.equals("5")) {
List. Add (VAL); // Error
// List. Remove (VAL); // Error
            }
        }
 

[Error cause]

  • For the remove operation, list. when removing (O), only modcount ++ is used, while the expectedcount value is not changed. When the iterator obtains the next element, it finds that the value of this binary value is not equal, and the concurrentmodificationexception is thrown.
  • For the add operation
  • See here for details: http://hi.baidu.com/sdausea/blog/item/57b2fa3dcb101908bba1672e.html

[Solution]

  • Remove: use the original ecology remove () provided by iterator ()
  •  

     

     

     

     

     

    Add: an error occurs when it is the same as remove. iterator does not provide the native add () method. Yes, you need to save the storage space with a new container, and then add it to the original container after the traversal is completed.

  • Set/list: for these two Common Containers, you can simply remove () and add () using the method described above.

  • Map: Use concurrenthashmap directly. Why can't other containers be used directly in a concurrent version ..? You can do it yourself.

[Correct Use Cases]

for (Iterator<String> it = list.iterator(); it.hasNext();) {
    String val = it.next();
    if (val.equals("5")) {
        it.remove(); 
    }
}
List<String> newList = new ArrayList<String>(); 
for (Iterator<String> it = list.iterator(); it.hasNext();) {
    String val = it.next();
    if (val.equals("5")) {
        newList.add(val);
    }
}
list.addAll(newList);

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.