RFR: 8005051: optimized defaults for Iterator.forEachRemaining
Paul Sandoz
paul.sandoz at oracle.com
Fri Apr 26 15:46:35 UTC 2013
On Apr 26, 2013, at 4:37 PM, Peter Levart <peter.levart at gmail.com> wrote:
>
> Another "interesting" usage:
>
> Iterator<?> i = ...;
>
> i.forEachRemaining(e -> {
> ...
> if (...) {
> i.forEachRemaining(dummy -> {}); // drain it
> }
> ...
> });
>
> It seems that mixing external and internal iteration in the same object is like a box of chocolates. For internal iteration, Iterable.forEach is more suitable than Iterator.forEachRemaining unless the later is specified to have the common state for both external and internal iteration, which means that internal can not be more effective than external.
>
Similar observations can apply to Spliterator as well. IIRC many spliterator.forEachRemaining implementations set up state to be fully traversed before looping. However, it is not possible in all cases.
I think we may have to document that a consumer must not interfere with the current traversal otherwise behaviour is undefined.
Paul.
More information about the core-libs-dev
mailing list