Eugene
Eugene

Reputation: 60184

How to combine a set of Observables into one Observable?

I have a List of data models, User in the example below, returned from a webService (Retrofit). I split the List into elements and emit them one by one so to filter them somehow. Then I need to combine all filtred Users and create a List of them. Please check out the code below so it is more clear.

webService.getUsers() // returns Observable<List<User>>
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .flatMap(users -> Observable.from(users)) // split the collection of Users, so we can filter them next step 
        .filter(user -> user.getId() % 2 == 0) // get only Users with even id
        // How to transform from here to have a List<User> ?

Upvotes: 1

Views: 1823

Answers (1)

kjones
kjones

Reputation: 5823

You can simply use the RxJava toList() operator after your filter() operator.

Upvotes: 5

Related Questions