简体   繁体   中英

ConcurrentModificationException when deleting an element from ArrayList

Java is throwing ConcurrentModificationException when I am running the following code. Any idea why is that?

ArrayList<String> list1 = new ArrayList<String>();
list1.add("Hello");
list1.add("World");
list1.add("Good Evening");

for (String s : list1){
        list1.remove(2);
    System.out.println(s);
}

If you take a look at documentation of ConcurrentModificationException you will find that

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it

...

Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception .

Important thing about this exception is that we can't guarantee it will always be thrown as stated in documentation

Note that fail-fast behavior cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast operations throw ConcurrentModificationException on a best-effort basis .

Also from ArrayList documentation

The iterators returned by this class's iterator and listIterator methods are fail-fast : if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException .

(emphasis mine)

So you can't manipulate content of Collection (in your case List) while iterating over it via enhanced for loop because you are not doing it via iterator for-each is using internally.

To solve it just get your own Iterator and use it in your loop. To remove elements from collection use remove like in this example

Iterator<String> it = list1.iterator();
int i=0;
while(it.hasNext()){
    String s = it.next();
    i++;
    if (i==2){
        it.remove();
        System.out.println("removed: "+ s);
    }
}

You can't delete an item with ArrayList 's remove() method while iterating over it. Use Iterator if you want to delete the item also while iterating.

But you are deleting an item based on index then simply moving below line outside the loop will solve your problem.

 list1.remove(2);

form the doc you can read

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible. For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.

further more, suppose that you can remove item 2 at each iteration. You will end up in a index out of bound exception:

  • at first iteration you remove item #2 ( "Good evening" ) and list size become 1 (item 0 "hello" and item 1 "World" )
  • at next iteration you remove item #2 which actually does not exist in your list. Your list is indeed of size two, but counter starts from 0, thus you end up removing something which is not there: this is an example of the non-deterministic behavior.

You cann't iterating over an list after the underlying list is modified.if you do that its give you ConcurrentModificationException

to resolve this issue use java.util.ListIterator for iteration of list.

    ListIterator<String> it = list1.listIterator();
    for (String s : list1) {
        if (it.hasNext()) {
            String item = it.next();
            System.out.println(item);
        }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM