RxJava2 Flowable all(条件操作符)

目录

all(条件操作符)

all图解

all测试用例

all分析

all实用场景


all(条件操作符

Single<Boolean> all(Predicate<? super T> predicate)

检验所有的由Publisher发出的项是否满足条件,如果满足Single会发出true的消息,反之则发出false的消息

all图解

all测试用例

测试1
private void doAll() {
        Flowable.just(1, 2,3,4,5,6,7,8,9,10).all(new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) throws Exception {
                if(integer <= 10) {
                    return true;
                }
                return false;
            }
        }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean) {
                    System.out.println("all number <= 10");
                } else {
                    System.out.println("not all number <= 10");
                }
            }
        });
    }
测试1结果:
10-02 19:51:25.294 10311-10311/hq.demo.net I/System.out: all number <= 10
10-02 19:51:28.315 10311-10342/hq.demo.net I/System.out: Done!

测试2
private void doAll() {
        Flowable.just(1, 2,3,4,5,6,7,8,9,11).all(new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) throws Exception {
                if(integer <= 10) {
                    return true;
                }
                return false;
            }
        }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean) {
                    System.out.println("all number <= 10");
                } else {
                    System.out.println("not all number <= 10");
                }
            }
        });
    }
测试2结果:
10-02 19:54:06.947 10563-10563/hq.demo.net I/System.out: not all number <= 10
10-02 19:54:09.971 10563-10614/hq.demo.net I/System.out: Done!

all分析

all 条件操作符 

会判断Flowable发射的每一个项目是否全部满足指定条件,全部满足返回true,否则返回false

上面通过just将1-11(10)的数字逐一发射出去,当所有的项都满足条件:x <= 10 ,信息消费者Consumer的accept回调函数返回aBoolean=true,否则aBoolean=false

all实用场景

(后续完善)

猜你喜欢

转载自blog.csdn.net/weixin_36709064/article/details/82933429