Stream#generate() vs. iterate()

Brian Goetz brian.goetz at oracle.com
Sat Oct 5 12:55:33 PDT 2013


Sequential and unordered are orthogonal characteristics.  

Ordered/unordered means that the source has a natural encounter order, and that stream operations should respect that order.  For example, lists and arrays have a natural encounter order; they have a first element, a second element, etc.  Whereas a HashSet has no natural encounter order; processing the elements in one order is as good as any other.  

Sequential/parallel has to do with whether the stream operations will execute sequentially or in parallel when the terminal operation is initiated. 

generate() returns a sequential, unordered stream.  You can turn that into a parallel, unordered stream with generate(f).parallel().  

On Oct 5, 2013, at 6:54 PM, Millies, Sebastian wrote:

> I'm a bit confused, perhaps it's just terminology:
> 
> Looking at the Javadoc (in b106) for Stream#generate(Supplier) it says it returns a sequential stream.
> In your post you say it returns an unordered stream. In what way can a sequential stream be unordered?
> 
> -- Sebastian
> 
> -----Original Message-----
> From: lambda-dev-bounces at openjdk.java.net [mailto:lambda-dev-bounces at openjdk.java.net] On Behalf Of Brian Goetz
> Sent: Saturday, October 05, 2013 6:13 PM
> To: Arne Siegel
> Cc: lambda-dev at openjdk.java.net
> Subject: Re: stream.parallel().limit() not usable
> 
> [snip]
> 
> You might also do better with Stream.generate, since it creates an unordered stream:
> 
>     Stream.generate(generatorFunction)
>                  .parallel()
>                  ...
> 
> 
> 
> 
> Software AG – Sitz/Registered office: Uhlandstraße 12, 64297 Darmstadt, Germany – Registergericht/Commercial register: Darmstadt HRB 1562 - Vorstand/Management Board: Karl-Heinz Streibich (Vorsitzender/Chairman), Dr. Wolfram Jost, Arnd Zinnhardt; - Aufsichtsratsvorsitzender/Chairman of the Supervisory Board: Dr. Andreas Bereczky - http://www.softwareag.com
> 
> 



More information about the lambda-dev mailing list