Can non terminal operation mark stream as ordered without actually sorting it ?

Boaz Nahum boaznahum at gmail.com
Sun Apr 7 08:55:39 PDT 2013


Hi.

I have parallel unordered stream.

In some point I convert the stream to Pair<Integer, T> where *Integer  *is
index of element in stream.
It is quite clear that each element is assigned arbitrary index, but once
assigned I want it to be ordered by this index.

Sure I can do:

si.sequential().map((t)->new Pair<>(index.incrementAndGet(), t));

But I already learnt that I cant count on 'sequential()' ...   :)

Or sort it:
si.map((t)->new Pair<>(index.incrementAndGet(), t)).sorted((t1,
t2)->t1.first.compareTo(t2.first));

But why pay the cost of sorting.

Or collect it to 'ordered' collection to protect 'sequential':
si.sequential().map((t)->new Pair<>(index.incrementAndGet(),
t)).collect(collector).stream();

here I pay the price of allocation Collection to hold all elements.


What i really need is somethings like this:

si.*ordered*().map((t)->new Pair<>(index.incrementAndGet(), t));

But unlike 'sequential()/parallel()' It cant be global.

Does making stream 'ordered' as better/worse performance than converting it
to stream->collection->stream ?

I know it is rare use, But still I'm wondering.

Thanks
Boaz


More information about the lambda-dev mailing list