javajava-8java-streamreduce

Using a reduce operation on a Java 8 Stream to group adjacent entries with the same key


I've got a java.util.stream.Stream containing key/value pairs like:

<1,3> <1,5> <3,1> <4,2> <4,7> <4,8>

Now I would like to merge all entries, which have the same key:

<1,[3,5]> <3,[1]> <4,[2,7,8]>

The data is already sorted, so only consecutive datasets have to be merged.

Now I'm searching for a way to transform the the content of the stream like above, without loading all datasets into memory.

I'd prefer to get a java.util.stream.Stream as result with a different object type containing a list of values instead of a single value.

My only approach is a custom iterator, which performs the merge, but it seems to be pretty ugly to convert to an iterator and back to a stream.

What is the best approach for it?


Solution

  • Here is the solution by SteamEx.

    int[][] datasets = { { 1, 3 }, { 1, 5 }, { 3, 1 }, { 4, 2 }, { 4, 7 }, { 4, 8 } };
    
    StreamEx.of(datasets) //
            .collapse((a, b) -> a[0] == b[0], groupingBy(a -> a[0], mapping(a -> a[1], toList()))) //
            .forEach(System.out::println);
    

    you can replace int[] with your dataset object. We can add peek to verify if it's lazy loading/calculation:

    StreamEx.of(datasets) //
            .peek(System.out::println) //
            .collapse((a, b) -> a[0] == b[0], groupingBy(a -> a[0], mapping(a -> a[1], toList()))) //
            .limit(1) //
            .forEach(System.out::println);