Search code examples
rx-javareactive-programmingproject-reactorreactivex

How to combine two Fluxes of Pairs sharing a equal element


I want to combine (zip) the following streams:

s1 = [1, A], [2, B], [3, C]

s2 = [1, X], [3, XX]

And get:

result = [1, Pair(A, X)], [2, Pair(B, null)], [3, Pair(C, XX)]

Is there a clean and efficient way to do this?

Thanks in advance for any help.


Solution

  • I guess I found the answer in https://blog.jooq.org/2015/08/13/common-sql-clauses-and-their-equivalents-in-java-8-streams. What I wanted was an inner join. Something like this:

    s1
      .flatMap(v1 -> s2
                       .filter(v2 -> v1.left().equals(v2.left()))
                       .map(v2 -> Pair.of(v1, Pair.of(v1.right(), v2.right())))