java RxJava2 过滤器列表<对象>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42214980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:27:47  来源:igfitidea点击:

RxJava2 filter List<Object>

javarx-javarx-androidrx-java2

提问by Bootstrapper

I'm trying to filter a List with RxJava2 such that each item (object) in the list should pass a validation check and I get a resulting List with only items that pass that test. For instance if my Object had the following structure,

我正在尝试使用 RxJava2 过滤列表,以便列表中的每个项目(对象)都应该通过验证检查,并且我得到一个结果列表,其中只有通过该测试的项目。例如,如果我的对象具有以下结构,

class MyClassA {
    int value1;
    int value2;
}

I want to only get the list of items where the value2is 10.

我只想获取value2值为 10的项目列表。

I have an API call function that returns an Observable of List, i.e. Observable<List<MyClassA>>as follows,

我有一个 API 调用函数,它返回一个列表的 Observable,即Observable<List<MyClassA>>如下,

apiService.getListObservable()
    .subscribeOn(Schedulers.io)
    .observeOn(AndroidSchedulers.mainThread());

and I would like to have the output filtered, so I tried adding a .filter()operator to the above but it seems to require a Predicate<List<MyClassA>>instead of just a MyClassAobject with which I can check and allow only ones where value2 == 10.

并且我想过滤输出,所以我尝试.filter()在上面添加一个运算符,但它似乎需要一个Predicate<List<MyClassA>>而不是一个MyClassA我可以检查的对象,并且只允许 where value2 == 10.

I'm pretty new to RxJava and RxJava2 and seems like I'm missing something basic here?

我对 RxJava 和 RxJava2 还很陌生,似乎我在这里遗漏了一些基本的东西?

TIA

TIA

回答by akarnokd

You can unroll the list and then collect up those entries that passed the filter:

您可以展开列表,然后收集通过过滤器的条目:

apiService.getListObservable()
.subscribeOn(Schedulers.io)
.flatMapIterable(new Function<List<MyClassA>, List<MyClassA>>() {
    @Override public List<MyClassA> apply(List<MyClassA> v) {
        return v;
    }
})
.filter(new Predicate<MyClassA>() {
    @Override public boolean test(MyClassA v) {
        return v.value2 == 10;
    }
})
.toList()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...);

回答by Pavan Kumar

You may take a look at the below. It demonstrates the ways to print just the filtered objects OR the lists that contain filtered objects. Here the filtering logic is to retain the org.apache.commons.lang3.tuple.Pairs that have even numbers in right.

你可以看看下面的内容。它演示了仅打印过滤对象或包含过滤对象的列表的方法。这里的过滤逻辑是保留org.apache.commons.lang3.tuple.Pair右边有偶数的s。

public static void main(String[] args) {
    // print raw output
    getListObservable().subscribe(System.out::println);

    // print the objects post filtering
    getListObservable().flatMap(v -> Observable.from(v)).filter(p -> p.getRight()%2==0).subscribe(System.out::println);

    // print the list refined with only filtered objects
    getListObservable().flatMap(v -> Observable.just(v.stream().filter(p -> p.getRight()%2==0).collect(Collectors.toList()))).subscribe(System.out::println);

}

private static Observable<List<Pair<Integer, Integer>>> getListObservable() {
    return Observable.create(subscriber -> {
        for(int i=0; i<5; i++){
            List<Pair<Integer, Integer>> list = new ArrayList<>();
            for(int j=0; j<5; j++){
                list.add(Pair.of(i, j));
            }
            subscriber.onNext(list);
        }
    });

}

Output with contents of observable:

带有可观察内容的输出:

[(0,0), (0,1), (0,2), (0,3), (0,4)]
[(1,0), (1,1), (1,2), (1,3), (1,4)]
[(2,0), (2,1), (2,2), (2,3), (2,4)]
[(3,0), (3,1), (3,2), (3,3), (3,4)]
[(4,0), (4,1), (4,2), (4,3), (4,4)]

Output to contain only filtered objects:

仅包含过滤对象的输出:

(0,0)
(0,2)
(0,4)
(1,0)
(1,2)
(1,4)
(2,0)
(2,2)
(2,4)
(3,0)
(3,2)
(3,4)
(4,0)
(4,2)
(4,4)

Output to contain the lists that contain only filtered objects.

输出以包含仅包含过滤对象的列表。

[(0,0), (0,2), (0,4)]
[(1,0), (1,2), (1,4)]
[(2,0), (2,2), (2,4)]
[(3,0), (3,2), (3,4)]
[(4,0), (4,2), (4,4)]