Filter list of objects in RxJava

后端 未结 4 1699
南方客
南方客 2021-02-13 07:00

I am trying to filter the list on the basis of it\'s property. For example, Sensors class has a property isActive and I want to get all the objects with isAct

4条回答
  •  粉色の甜心
    2021-02-13 07:08

    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)
    
    

提交回复
热议问题