You change the list as you scroll.
Here, ConcurrentModificationException has nothing to do with threads.
Attention! The following example is provided here to show how dangerous it can be to use a collection when changing it. This is not exactly what happened in the case of OP (loop through iterator and change)
I think it would be easier to imagine what is wrong with him if you use an old-fashioned loop with such a counter:
List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c")); for (int i = 0; i < list.size(); i++) { list.remove(i);
Now the first time i is 0, and the 0th element has been deleted. The second time i is 1, so now the 1st element is removed. But, what is now 1st, it used to be 2nd, before the first removal. So, what used to be 1, and now 0, will never be deleted.
Goran jovic
source share