Iterator.forEachRemaining and Iterator.remove

Martin Buchholz martinrb at
Mon Oct 24 19:32:40 UTC 2016

It doesn't make a lot of sense for users to call Iterator.remove after
calling Iterator.forEachRemaining.

The default implementation has no choice but to do

        while (hasNext())

upon which remove() removes the last element.  What should overriding
implementations do?  Emulate the default implementation's behavior or
throw IllegalStateException, or even remove the element returned by the
last actual call to next?  The spec is (perhaps intentionally) unclear on
this point.  I'm thinking we emulate the default implementation's behavior,
because common collections like ArrayList work this way, and some users may
be depending on it, perhaps unwisely.

More information about the core-libs-dev mailing list