Proposal: JDK-8148917 Enhanced-For Statement Should Allow Streams

Stephen Colebourne scolebourne at
Mon Mar 4 14:16:31 UTC 2019

On Fri, 1 Mar 2019 at 02:46, Stuart Marks <stuart.marks at> wrote:
> Please review and comment on this proposal to allow Stream instances to be used
> in enhanced-for ("for-each") loops.

This all seems reasonable enough though of course not ideal.

My slight concern is that the terminal operation is hidden and not
immediately visible, which could be surprising. I do note that streams
throw a clear exception if a terminal operation is called a second
time which mitigates this to some degree.

The IterableOnce class-level Javadoc contradicts the method-level
Javadoc. The class-level say "It is recommended that" whereas the
method-level Javadoc mandates.


> Abstract
> Occasionally it's useful to iterate a Stream using a conventional loop. However,
> the Stream interface doesn't implement Iterable, and therefore streams cannot be
> used with the enhanced-for statement. This is a proposal to remedy that
> situation by introducing a new interface IterableOnce that is a subtype of
> Iterable, and then retrofitting the Stream interface to implement it. Other JDK
> classes will also be retrofitted to implement IterableOnce.
> Full Proposal:
> Bug report:
> Webrev:
> Note, this changeset isn't ready to push yet. In particular, it has no tests
> yet. However, the implementation is so simple that I figured I should include
> it. Comments on the specification wording are also welcome.
> Thanks,
> s'marks

More information about the core-libs-dev mailing list