过滤RxJava中的对象列表 [英] Filter list of objects in RxJava

查看:65
本文介绍了过滤RxJava中的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据其属性来过滤列表.例如,Sensors类具有属性 isActive ,我想使用 isActive 作为 true 的所有对象,但是我无法做到.我尝试了不同的方法,但没有找到解决方案.有人可以帮我做吗?

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?

这是我的代码:

mCompositeDisposable.add(
    fcService.getStationList()
    .subscribeOn(Schedulers.io())
    .flatMap(stations -> {
        return fcService.getSensorList(stations.get(0).getName().getOriginal());}
    ).subscribe(this::handleSensors, this::handleError));

推荐答案

首先,您需要分别从 List 中发出每个项目.可以使用 flatMap()

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

然后应用 filter() 运算符.最后,使用 toList() .

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)

这篇关于过滤RxJava中的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆