How to Avoid "Concurrentmodificationexception" While Removing Elements from 'Arraylist' While Iterating It

How to avoid ConcurrentModificationException while removing elements from `ArrayList` while iterating it?

Use an Iterator and call remove():

Iterator<String> iter = myArrayList.iterator();

while (iter.hasNext()) {
String str = iter.next();

if (someCondition)
iter.remove();
}

ConcurrentModificationException while trying to delete an item from ArrayList

You cannot call remove() from inside a "for-each" loop (the for (item : collection) structure). Doing so will throw that ConcurrentModificationException.

If you want to loop and remove items while looping, you can use a traditional "for" loop:

for (int i = 0; i < itemStorico.size(); ++i) {
ItemModel itemModel2 = itemStorico.get(i);
if (...) {
itemStorico.remove(i);
...
}
}

However, this will introduce a subtle bug. Imagine you're looping over [a,b,c,d]. If you are at index 1 and remove b from the collection, everything will "slide over" and you'll have [a,c,d]. When you next look at index 2, you'll have skipped over c.

You can avoid the bug by manually decrementing the index when you remove, but it's a little gross.

...
itemStorico.remove(i);
--i;
...

You can also avoid the problem altogether by using the collection's iterator().

for (Iterator<ItemModel> iterator = itemStorico.iterator(); iterator.hasNext(); ) {
ItemModel itemModel2 = iterator.next();
if (...) {
iterator.remove();
...
}
}

ConcurrentModificationException when removing item from a list

ConcurrentModificationException means:

  1. At point in time A, you make an iterator of some collection either by invoking its .iterator() method, or having a for (var x : collection) {} call it for you.
  2. At point in time B, you change the collection (and not via the iterator you made in A's .remove() method), for example by invoking remove or add or clear or retainAll.
  3. At point in time C, you so much at look funny at that iterator: You invoke any of its methods, or you have the for loop do it by hitting the } of its block.

What you need to do is decidedly nontrivial!

Think about it, given an initial list of [A, B, C, D, E]: you'd presumably want that forEachWithIndex method to get run 5 times, regardless of what happens to the list in between: [0, A], [1, B], [2, C], [3, D], and [4, E]. So what should happen if, during the loop for [0, A], you remove C?

There's an argument to be made that the [2, C] event shouldn't happen at all, and, in fact, that the remaining loops should be [1, B], [2, D], and [3, E]. It's because this is so hard to answer that java solved the problem in the iterator() API by simply not allowing you to do it!

Similar question comes up when you call .add("F") during the loop for [0, A]. Should the for loop run the lambda once with arguments [5, F], or not? An open question.

It's up to you to answer this question, and you should document this in excruciating detail. No matter which choice you make it'll be quite difficult!

I think the for loop should include the changes

This is incredibly complicated. Because imagine that the loop for C ends up removing A. That means that your list will first invoke the lambda with arguments [0, A], [1, B], and [2, C], and then, what's the next iteration going to look like? Presumably the only sane answer then is [2, D]. To make this work you need to track all sorts of things - the list's loop code needs to be aware that deletions happened and that therefore it needs to 'down-adjust' (because you can't simply loop from 0 to 'list size', if you did that, the next iteration would be [3, E] and you have skipped D entirely even though it is still in that list.

Make some coffee, dig in, find a whiteboard, sketch this out. Reserve a full day and be aware that the code will be many pages to deal with it all. Make a TON of test cases and describe in detail precisely what you expect to happen for all of these.

Hmm, okay, nevermind. Let's say it should iterate for all elements the original had no matter what changes

This is easier but inefficient. The fix is simple: First make a copy of the list. Then iterate the copy. The copy cannot change (you're the only one with a ref to it), so they can do whatever they want to the underlying list:

XList<T> copy = new XList<T>(this);
int counter = 0;

var iterator = copy.iterator();
while (iterator.hasNext()) consumer.accept(iterator.next(), counter++);

ConcurrentModificationException for ArrayList

You can't remove from list if you're browsing it with "for each" loop. You can use Iterator. Replace:

for (DrugStrength aDrugStrength : aDrugStrengthList) {
if (!aDrugStrength.isValidDrugDescription()) {
aDrugStrengthList.remove(aDrugStrength);
}
}

With:

for (Iterator<DrugStrength> it = aDrugStrengthList.iterator(); it.hasNext(); ) {
DrugStrength aDrugStrength = it.next();
if (!aDrugStrength.isValidDrugDescription()) {
it.remove();
}
}

How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList

Two options:

  • Create a list of values you wish to remove, adding to that list within the loop, then call originalList.removeAll(valuesToRemove) at the end
  • Use the remove() method on the iterator itself. Note that this means you can't use the enhanced for loop.

As an example of the second option, removing any strings with a length greater than 5 from a list:

List<String> list = new ArrayList<String>();
...
for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
String value = iterator.next();
if (value.length() > 5) {
iterator.remove();
}
}

Removing elements while iterating. removeIf result in ConcurrentModificationException

Don't iterate and removeIf using elements of your iteration. Beside the problem you're experiencing right now, those calls amount to iterating through the entire collection for each element of the collection (so you're still removing from the collection while iterating, which explains the exception!).

removeIf iterates for you, so all you need is a predicate of SomeObject:

//no loop
someObjectSet.removeIf(ele -> if ele satisfies some condition);

Where ele -> if ele satisfies some condition is the condition that each SomeObject element will be tested against (the ones passing the test will be removed). forEach will orchestrate the test on all elements in someObjectSet, you don't need to do that.


If you're having a secondary condition based on which you want to remove elements, then you can compose predicates (with or), something like in this example:

Set<Integer> set = new HashSet<>(Set.of(1, 2, 3, 4, 5, 6, 7, 8, 9));

Predicate<Integer> predicate = s -> s % 2 == 0;
Predicate<Integer> predicate2 = predicate.or(s -> s % 3 == 0);
set.removeIf(predicate2);

// Test with set.removeIf(predicate);
// then with set.removeIf(predicate2);
// and compare results

Getting a ConcurrentModificationException thrown when removing an element from a java.util.List during list iteration?

I believe this is the purpose behind the Iterator.remove() method, to be able to remove an element from the collection while iterating.

For example:

Iterator<String> iter = li.iterator();
while(iter.hasNext()){
if(iter.next().equalsIgnoreCase("str3"))
iter.remove();
}

Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop

Iterator.remove() is safe, you can use it like this:

List<String> list = new ArrayList<>();

// This is a clever way to create the iterator and call iterator.hasNext() like
// you would do in a while-loop. It would be the same as doing:
// Iterator<String> iterator = list.iterator();
// while (iterator.hasNext()) {
for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
String string = iterator.next();
if (string.isEmpty()) {
// Remove the current element from the iterator and the list.
iterator.remove();
}
}

Note that Iterator.remove() is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.

Source: docs.oracle > The Collection Interface


And similarly, if you have a ListIterator and want to add items, you can use ListIterator#add, for the same reason you can use Iterator#remove — it's designed to allow it.


In your case you tried to remove from a list, but the same restriction applies if trying to put into a Map while iterating its content.

How do I avoid ConcurrentModificationException in ArrayList ONLY when iterating?

The issue comes from the fact that masterList is a reference to either contains or doesNotContain after a first split. When you iterate on masterList, you actually also iterate at the same time on that other list.

So, then you add items to the lists:

if(i.contains(guessString)){
contains.add(i);
}else{
doesNotContain.add(i);
}

Here you do not only add items to contains or doesNotContain, but also potentially to masterList, which leads to the conccurentException.


To solve your issue, just make a copy of your lists, instead of : masterList = contains;

do a copy with: masterList = new ArrayList<>(contains);

And the same for doesNotContains.


Another solution which comes to mind is to reset the two lists contains and doesNotContains for each split. Since you only use them in this method, and nowhere else, remove these two lists from your Class, and defines them as private variables inside splitFamilies



Related Topics



Leave a reply



Submit