2

Say i have two Flux as follows:

    Flux<Integer> f1 = Flux.just(10,20,30,40);
    Flux<Integer> f2 = Flux.just(100,200,300,400);

Now what i want is to combine these flux into a single Flux or a Tuple of both the Flux, which will be having the elements of both the Flux in a single Flux.

I tried the following using the zipwith method:

  Flux<Integer, Integer> zipped = f1.zipWith(f2,
                                            (one, two) ->  one + "," +two)
                                    .subscribe();

But this is giving a compile time error:

 Incorrect number of arguments for type Flux<T>; it cannot be parameterized with arguments <Integer, Integer>

How can i achieve this? Please suggest.

Oleh Dokuka
  • 11,613
  • 5
  • 40
  • 65
KayV
  • 12,987
  • 11
  • 98
  • 148

1 Answers1

5

Flux only has a single type argument, so Flux<Integer,Integer> is impossible, and I'm not sure what you're trying to achieve with one + "," + two, but the type of this expression is String.

So, in essence, you're mapping two integers to a String, so the type of zipped should be Flux<String>.

Alternatively, you could map to a special tuple class of your own making (or maybe on from a library you're using):

public class Pair<A,B> {
    private final A first;

    private final B second;

    public Pair(A first, B second) {
        this.first = first;
        this.second = second;
    }

    public A getFirst() {
        return first;
    }

    public B getSecond() {
        return second;
    }
}

You can then map this as follows:

Flux<Integer> f1 = Flux.just(10,20,30,40);
Flux<Integer> f2 = Flux.just(100,200,300,400);

Flux<Pair<Integer,Integer>> result = f1.zipWith(f2,
    (one, two) ->  new Pair<>(one, two));

Or even shorter:

Flux<Pair<Integer,Integer>> result = f1.zipWith(f2, Pair::new);
Jeroen Steenbeeke
  • 3,884
  • 5
  • 17
  • 26