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实用场景

(后续完善)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值