Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.
Let's look at removing and modifying elements in more detail.
4.1. Removing an Element
Many collections (e.g. ArrayList or HashSet) shouldn't be structurally modified while iterating over them. If an element is removed or added during an iteration, we'll get a ConcurrentModification exception.
Furthermore, collections are designed to fail fast, which means that the exception is thrown as soon as there's a modification.
Similarly, we'll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later.
Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements using the iterator. Streams, in contrast, should be non-interfering.