Rxjava 条件操作符


作用:从事件中是不是 满足 得 到结果( 一个)

1 all

2 contains

3 any

4 isEmpty

5 defaultIfEmpty

6 skipWhile

1 all

判断发送的每项数据是否都满足 设置的函数条件

判断发送的数据中是否包含指定数据

若包含,返回 true;否则,返回 false
内部实现 = exists()

若满足,返回 true;否则,返回 false

    @Test
    public void testAll() throws Exception {
        Observable.just(8, 5, 3, 4, 5).all(new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) throws Exception {
                return integer > 2;
            }
        }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                System.out.println(aBoolean);
            }
        });
    }

输出结果为 true

如果:return integer > 3;则输出false


2 contains

    @Test
    public void testContains() throws Exception {
        Observable.just(3, 4, 5, 6).contains(5).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                System.out.println(aBoolean);
            }
        });
    }

输出结果:true


3 any

 @Test
    public void testAny() throws Exception {
        Observable.just(1, 2, 3).any(new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) throws Exception {
                return integer == 3;
            }
        }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                System.out.println(aBoolean);
            }
        });
    }

输出结果为 true


4 isEmpty

判断发送的数据是否为空

若为空,返回 true;否则,返回 false

 @Test
    public void testIsEmpty() {

        Observable.just(1, 2, 3, 4, 5, 6).isEmpty().subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                System.out.println(aBoolean);
            }
        });

        Observable.empty().isEmpty().subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                System.out.println(aBoolean);
            }
        });
    }

输出结果:

false
true


5 defaultIfEmpty

在不发送任何有效事件( Next事件)、仅发送了 Complete 事件的前提下,发送一个默认值

  @Test
    public void defaultEmpty() {

        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> e) throws Exception {
                // 不发送任何有效事件
                //  e.onNext(1);
                //  e.onNext(2);

                // 仅发送Complete事件
                e.onComplete();
            }
        }).defaultIfEmpty(10) // 若仅发送了Complete事件,默认发送 值 = 10
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        System.out.println("开始采用subscribe连接");
                    }

                    @Override
                    public void onNext(Integer value) {
                        System.out.println("接收到了事件" + value);
                    }

                    @Override
                    public void onError(Throwable e) {
                        System.out.println("对Error事件作出响应");
                    }

                    @Override
                    public void onComplete() {
                        System.out.println("对Complete事件作出响应");
                    }
                });

    }

开始采用subscribe连接
接收到了事件10
对Complete事件作出响应


6 skipWhile

判断发送的每项数据是否满足 设置函数条件

直到该判断条件 = false时,才开始发送Observable的数据

    @Test
    public void testSkipWhile() throws Exception {
//        Observable.intervalRange(0, 5, 0, 100, TimeUnit.MILLISECONDS)
//                .skipWhile(new Predicate<Long>() {
//                    @Override
//                    public boolean test(Long aLong) throws Exception {
//                        return aLong < 2;
//                    }
//                }).subscribe(new Consumer<Long>() {
//            @Override
//            public void accept(Long aLong) throws Exception {
//                System.out.println(aLong);
//            }
//        });


        Observable.just(1,2,3,4,5,6,7,8).skipWhile(new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) throws Exception {
                return integer<4;
            }
        }).subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                System.out.println(integer + "");
            }
        });

    }

4
5
6
7
8


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值