5
我使用java8流。下面是數據結構,我有:java8 for each循環期間的java.util.ConcurrentModificationException
Map< String, List<String>> mmessage = getSomeMessage();
然後我通過地圖和列表迭代:
mmessage.entrySet().stream().forEach(entry -> {
entry.getValue().stream().forEach(li -> {
if (lis.indexOf(li) == - 1) {
lis.add(lineItem);
}
});
});
,但得到的併發修改例外:
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)
從我的理解通過迭代map \ list不應該導致這種行爲。
什麼是'lis',它與mmessage' Map'有什麼關係? – Eran
您不能在您正在迭代的列表中添加valies – Jens
@Jens OP不會將值添加到同一個列表中(至少我們還不知道,因爲我們不知道什麼是'lis')。 – Eran