stream.parallel().limit() on large streams
Paul Sandoz
paul.sandoz at oracle.com
Tue Oct 8 02:02:41 PDT 2013
On Oct 8, 2013, at 10:12 AM, "Millies, Sebastian" <Sebastian.Millies at softwareag.com> wrote:
> BTW, does it make any difference where the call to unordered() is placed in the chain?
Yes, since the sorted operation can inject order, or other stateful operations might behave differently if their input is ordered or not, e.g. limit.
unordered() clears an ORDERED flag (if set).
> Or is it all the same stream anyway, and the UNORDERED-property need only be set at some
> arbitrary point before the terminal operation triggers processing? Can I have a chain
> partway processed in an order-respecting way, and partway processed in an unordered way?
Yes.
> Would that be good style (could anyone even think of an example when might want
> that behavior?).
>
It's not really about style but about what do you want to do.
// Noddy example
// Get any 5 elements, sort them and map to sum of squares (in order)
Set<Integer> s = IntStream.range(0, 20).boxed().collect(toSet());
List<Integer> l = s.stream().parallel().limit(5).sorted().map(e -> e * e).collect(toList());
I suspect that a general rule for unordered() is to place it as far left of the pipeline as is allowable for the result you want to obtain.
Paul.
More information about the lambda-dev
mailing list