Stream from Iterable

Brian Goetz brian.goetz at oracle.com
Tue Dec 11 08:06:21 PST 2012


I do not expect this to be a common operation; clients will obtains 
streams by calling the stream() method on a Streamable.  This technique 
is a lower-level API for *implementors* of Streamable.  And I think 
you'll agree that this is far, far less verbose than what you have to do 
to implement Iterable...

The #1 source of Iterables is Collections.  All the Collections have 
stream() methods; their implementations are one-liners like this one.

Actually, this is one of the weaker ways to implement a Stream; if you 
have a real data structure, you'll probably want to implement 
Spliterator.  But if all you have is an Iterator, we can turn it into a 
stream for you.

On 12/11/2012 10:27 AM, Stephen Colebourne wrote:
> On 11 December 2012 14:29, Brian Goetz <brian.goetz at oracle.com> wrote:
>> Try this:
>>
>> Streams.stream(Streams.spliterator(iter.iterator(), sizeIfKnown),
>>                  flags);
>>
>> You might want ORDERED as a flag.
>
> That seems quite verbose for what seems like a common operation...
>
> Stephen
>


More information about the lambda-dev mailing list