Performing an intermediate sorted bulk operation on a Java stream

I have Java Stream

objects like this:

{ 1, 2 }
{ 1, 6 }
{ 2, 3 }
{ 3, 10 }
{ 3, 12 }
{ 3, 20 }

      

These tuples are guaranteed to be ordered by their first element. I want to convert this stream to another stream of tuples, for example:

{ 1, [2, 6] }
{ 2, [3] }
{ 3, [10, 12, 20] }

      

But, here's the key, I want it to be an intermediate operation - I don't have to evaluate the entire first stream to output each group, because the tuples are already ordered as needed. I want something like an intermediate collector, but I cannot find a way to do this in the streaming API.

Can anyone think of this without looping through the whole stream?

+3


source to share





All Articles