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 isActive
as true
but I am unable to do it. I tried different ways but I didn't find the solution. Can someone help me to do it?
Here is my code:
mCompositeDisposable.add(
fcService.getStationList()
.subscribeOn(Schedulers.io())
.flatMap( stations -> {
return fcService.getSensorList(stations.get(0).getName().getOriginal());
})
.subscribe(this::handleSensors, this::handleError)
);
First, you need to emit each item from the
List
individually. That can be achieved usingflatMap()
andObservable.fromIterable(Iterable)
.Then apply
filter()
operator. Lastly, collect all of those items into list again usingtoList()
.Kotlin users can use a more simple approach like below.
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.