RxJava2 Flowable filter (过滤操作符)

filter(过滤操作符)

目录

1 filter作用

2 filter接口

3 filter图解说明

4 filter测试用例


 

1 filter作用

通过指定的条件来过滤发布者发出的项。

 

2 filter接口

Flowable<T> filter(Predicate<? super T> predicate)

Filters items emitted by a Publisher by only emitting those that satisfy a specified predicate.

通过指定的条件来过滤发布者发出的项。

 

3 filter图解说明

RxJava2 Flowable filter (过滤操作符)

会过滤掉值小于10的项

 

4 filter测试用例

测试用例中会过滤掉源Publisher发射的非item1和item7的项目

 @Test
    public void filter() {
        System.out.println("######filter#####");
        Flowable.just("item2", "item1", "item7", "item8", "item9").filter(new Predicate<String>() {
            @Override
            public boolean test(String s) throws Exception {
                return s.equals("item1") || s.equals("item7");
            }
        }).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                System.out.println("s = " + s);
            }
        });
    }


测试输出
######filter#####
s = item1
s = item7