I have been using RxJava 1 for some time but I want to look at RxJava 2. In RxJava 1, I could emit each item of list as follows:
List<String> list = ...
Observable.from(list)
.filter(str -> str.contains("Help")
.subscribe(...);
However, how can I achieve the same with RxJava2? I have tried to use the following but I can't seem to get past the following:
Observable.fromArray(list)
// this now passes a list into the stream - there is no Observable::from
flatMapIterable to the rescue! This handy operator flattens a stream of Iterables into a stream generated from the single items of these Iterables by means of a mapping function.
You need to use fromIterable()
since any List<T>
extends Collection<T>
which extends Iterable<T>
Observable.fromIterable(list)
.filter(str -> str.contains("Help")
.subscribe(...);
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