Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flatten a list in RxJava 2

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
like image 301
blackpanther Avatar asked Jan 08 '17 19:01

blackpanther


People also ask

What does flatMapIterable do?

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.


1 Answers

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(...);
like image 79
Alexander Perfilyev Avatar answered Sep 23 '22 11:09

Alexander Perfilyev