Consumer<Integer> removeElement = s -> {
System.out.println(s + " " + list.size());
if (s != null && s==1) {
list.remove(4);
}
};
收集forEach ()
list.forEach(removeElement);
由于forEach()是快速失败的,我们停止迭代并在下一个元素被处理之前看到一个异常:
1 4
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList.forEach(ArrayList.java:1252)
at ReverseList.main(ReverseList.java:1)
流forEach ()
list.stream().forEach(removeElement);
1 4
2 3
3 3
null 3
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1380)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at ReverseList.main(ReverseList.java:1)