Search code examples
javaparallel-processingjava-8java-stream

Joining two java streams


I have two java lists like this:

"type 1" list:
[(id:1, type: 1, value: 100), (id:2, type: 1, value: 50), ...]
"type 2" list:
[(id:1, type: 2, value: 150), (id:2, type: 2, value: 70), ...]

And I want something like that:

Stream.concat(list1.stream(), list2.stream())
.parallel()
// I need to combine somehow items with the same id for following process
// ideally to have Tuple2(l1item, l2item) after that
.groupBy(x -> x.getId())
.map ((l1item, l2item) -> {
   // some processing
}).collect(Collectors.toList())

AFAIK it can be implemented via:

Stream.concat(list1.stream(), list2.stream())
.collect(groupingBy(x -> x.getItem)).values().stream()
.map (listOftwo -> ....)

But I don't want that intermediate map. Any ideas ? I can use any library.

Thanks


Solution

  • abacus-common provides the APIs to do exactly what you want:

    Stream.concat(a, b).parallel().groupBy(x -> x.getId()).map(..);
    

    Declaration: I'm the developer of AbacusUtil.