I am kinda new to RxJava and I am trying to implement search function that searches local db and the server. I would like it merge both results and eliminate the duplicates, any ideas ??
You can use the distinct operator.
You can merge the local and remote results and use toMap
to eliminate the duplicates.
Moreover, if you have more requirements, you can use collect
and HashSet
(or HashMap
) which give you more control:
Observable<Integer> local = Observable.just(1, 2, 3, 4);
Observable<Integer> remote = Observable.just(1, 3, 5, 7);
local.mergeWith(remote)
.collect(() -> new HashSet<Integer>(), (set, v) -> set.add(v))
.flatMap(Observable::from)
.subscribe(System.out::println);
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