Is it possible to get Cartesian product of two Observables in rxjava?
Something like this:
A -> 1,2,3
B -> a,b
A x B -> (1, a), (1, b), (2, a), (2, b), (3, a), (3, b)
What you want is to produce, for one number, as pair as you have letters.
So you can easily achive this using map
and flatMap
operator.
Observable<Integer> number = Observable.from(Arrays.asList(1, 2, 3));
Observable<String> letters = Observable.from(Arrays.asList("a", "b"));
Observable<Pair> cartesian = number.flatMap((n) -> letters.map((l) -> new Pair(n, l)));
Is "join" not what you looking for?
Observable<Integer> a = Observable.fromArray(1, 2, 3);
Observable<String> b = Observable.fromArray("a", "b");
Observable<?> left = Observable.never();
Observable<?> right = Observable.never();
Observable<Pair<Integer, String>> pairs = a.join(b, (aa)->left, (bb)->right, (ai, bi)-> Pair.of(ai, bi));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With