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.
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.
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 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.
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.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With