Filter list of objects in RxJava

Kotlin users can use a more simple approach like below.

fcService.getStationList()
    .map(it.filter {sensor -> sensor.isActive()})

It is possible because Kotlin has so many list operators itself, so for the filtering part you don't have to use rx. it.filter {sensor -> sensor.isActive()} is pure Kotlin code.


First, you need to emit each item from the List individually. That can be achieved using flatMap() and Observable.fromIterable(Iterable).

Then apply filter() operator. Lastly, collect all of those items into list again using toList().


    service.getSensorsList()
              .flatMap(Observable::fromIterable)
              .filter(sensor -> sensor.isActive())
              .toList()
              .subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(this::handleSensors, this::handleError)