Iterator.forEachRemaining and Iterator.remove

Paul Sandoz paul.sandoz at
Tue Oct 25 23:13:57 UTC 2016

> On 25 Oct 2016, at 15:16, Martin Buchholz <martinrb at> wrote:
> Actually, the ArrayList implementation updates fields only at the end of the iteration, so if an action throws in the middle, the iterator is semi-corrupted (in the sense that remove() will remove the "wrong" element and next will return previously visited elements).

Good point.

> I think it's best to say in the spec that after calling forEachRemaining, subsequent operations on the Iterator are undefined (whether or not the action throws).

Yes (and for an action calling remove).

>   For consistency, we should probably keep the historic behavior that remove() after forEachRemaining() removes the last element, even though that is not to be encouraged.



More information about the core-libs-dev mailing list