Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java8 java.util.ConcurrentModificationException during forEach cycle

I use java8 streams. Here is the data structure I have:

Map< String, List< String >> mmessage = getSomeMessage();

Then I iterate via the map and list:

 mmessage.entrySet().stream().forEach( entry -> {
            entry.getValue().stream().forEach( li -> {
                if ( lis.indexOf( li ) == - 1 )  {
                    lis.add( lineItem );
                }
            });
        });

But get concurrent modification exception:

java.util.ConcurrentModificationException
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1380)
    at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
    at com.web3.buyer.roomba.RoombaTurn.lambda$received$3(RoombaTurn.java:296)
    at java.util.Iterator.forEachRemaining(Iterator.java:116)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
    at com.web3.buyer.roomba.RoombaTurn.received(RoombaTurn.java:295)
    at com.web3.buyer.SystemBus.lambda$publishToTheQueue$0(SystemBus.java:51)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

From my understanding iterating via the map \ list should not cause this kind of behavior.

like image 752
Yuli Reiri Avatar asked Aug 01 '16 09:08

Yuli Reiri


People also ask

How do I fix Java Util ConcurrentModificationException?

How do you fix Java's ConcurrentModificationException? There are two basic approaches: Do not make any changes to a collection while an Iterator loops through it. If you can't stop the underlying collection from being modified during iteration, create a clone of the target data structure and iterate through the clone.

How can you avoid ConcurrentModificationException while iterating a collection?

To Avoid ConcurrentModificationException in single-threaded environment. You can use the iterator remove() function to remove the object from underlying collection object. But in this case, you can remove the same object and not any other object from the list.

What causes Java Util ConcurrentModificationException?

What Causes ConcurrentModificationException. The ConcurrentModificationException generally occurs when working with Java Collections. The Collection classes in Java are very fail-fast and if they are attempted to be modified while a thread is iterating over it, a ConcurrentModificationException is thrown.

What is ConcurrentModificationException and how it can be prevented?

ConcurrentModificationException is a predefined Exception in Java, which occurs while we are using Java Collections, i.e whenever we try to modify an object concurrently without permission ConcurrentModificationException occurs which is present in java. util package.


1 Answers

I would write this using a full functional style and you shouldn't run into the problem of modifying a list while iterating it.

List<String> strs = mmessage.values().stream()
                            .flatMap(List::stream)
                            .distinct()
                            .collect(Collectors.toList());
like image 187
Peter Lawrey Avatar answered Oct 10 '22 14:10

Peter Lawrey