0

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);
}
Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
user3520698
  • 77
  • 2
  • 9
  • possible duplicate of [ConcurrentModificationException for ArrayList](http://stackoverflow.com/questions/3184883/concurrentmodificationexception-for-arraylist) – DNA Apr 19 '14 at 13:40

4 Answers4

0

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);
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Look at the output its says "removed" for all items. – Braj Apr 19 '14 at 13:34
  • Erick is also giving similar explanation (though in a shorter format :) [http://stackoverflow.com/questions/9469210/why-is-this-arraylist-throwing-a-concurrentmodificationexception-when-i-try-to-r?rq=1] – user3520698 Apr 19 '14 at 14:19
  • Yes, now that I've searched over SO for `ArrayList` and `ConcurrentModificationException` I found many questions with shorter answers :) – Pshemo Apr 19 '14 at 14:22
0

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);
Tech Expert Wizard
  • 365
  • 1
  • 6
  • 21
Braj
  • 46,415
  • 5
  • 60
  • 76
  • 1
    i agree, it works with Iterator. But, why does not it give me an error when i add an if statement in the same loop. here is the code. for (String s : list1){ if(s.equals("World")){ list1.remove(1); } } – user3520698 Apr 19 '14 at 13:33
  • Try this one `for (String s : list1){ if(s.equals("Hello")){ list1.remove(1); } }` You can't delete an item that is not processed yet or you can say whose index is greater than current running index of for-each loop. – Braj Apr 19 '14 at 13:40
  • I am not sure but its a good question. Let me try to find out the exact reason. – Braj Apr 19 '14 at 13:44
  • @user3520698 As I pointed in my answer throwing this exception cannot be guaranteed. Fail-fast operations throw ConcurrentModificationException on a best-effort basis. One of the reasons for this could be caching modification counter by threads (if you take a look at `modCount` in `ArrayList` class you will not see `volatile` modifier which could prevent this value from being cached). – Pshemo Apr 19 '14 at 14:03
0

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.
LMG
  • 966
  • 1
  • 12
  • 28
0

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);
        }
        }
loknath
  • 1,362
  • 16
  • 25