RxJava2.0(二)常用操作符

过滤操作符

filter:

功能:过滤掉不需要的数据,往观察者中传入需要的数据。

例子:

    Observable.just(1,2,3,4,5,6,7,8,9)
                .filter(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 {
                Log.i("result:",integer + "");
            }
        });
复制代码
结果:

2018-11-29 17:35:02.329 5378-5378/com.example.qubin.demo I/result:: 5
2018-11-29 17:35:02.329 5378-5378/com.example.qubin.demo I/result:: 6
2018-11-29 17:35:02.329 5378-5378/com.example.qubin.demo I/result:: 7
2018-11-29 17:35:02.329 5378-5378/com.example.qubin.demo I/result:: 8
2018-11-29 17:35:02.329 5378-5378/com.example.qubin.demo I/result:: 9
复制代码

可以看到我们这里把小于4的数据过滤掉了,只留下了大于4的数据


distinct:

功能:过滤掉重复内容

例子:

 Observable.just(1,2,3,1,4,5,3)
                .distinct()
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.i("result:",integer + "");
                    }
                });
复制代码

结果:

2018-11-29 17:39:27.084 5754-5754/com.example.qubin.demo I/result:: 1
2018-11-29 17:39:27.084 5754-5754/com.example.qubin.demo I/result:: 2
2018-11-29 17:39:27.084 5754-5754/com.example.qubin.demo I/result:: 3
2018-11-29 17:39:27.084 5754-5754/com.example.qubin.demo I/result:: 4
2018-11-29 17:39:27.084 5754-5754/com.example.qubin.demo I/result:: 5
复制代码

可以看到,此处把重复的1和3给去掉了。


distinctUntilChanged

功能:过滤掉重复内容


            Observable.just(1,2,3,4,5,5,4,2,2,1)
                .distinctUntilChanged()
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.i("result:",integer + "");
                    }
                });
复制代码
结果:

2018-11-29 17:41:49.018 6027-6027/com.example.qubin.demo I/result:: 1
2018-11-29 17:41:49.018 6027-6027/com.example.qubin.demo I/result:: 2
2018-11-29 17:41:49.018 6027-6027/com.example.qubin.demo I/result:: 3
2018-11-29 17:41:49.018 6027-6027/com.example.qubin.demo I/result:: 4
2018-11-29 17:41:49.018 6027-6027/com.example.qubin.demo I/result:: 5
2018-11-29 17:41:49.018 6027-6027/com.example.qubin.demo I/result:: 4
2018-11-29 17:41:49.018 6027-6027/com.example.qubin.demo I/result:: 2
2018-11-29 17:41:49.018 6027-6027/com.example.qubin.demo I/result:: 1

复制代码

延时操作符

interval:

功能:定时器,轮询。默认在子线程进行轮询,可自行切换到主线程

注意:默认在子线程进行轮询,可自行切换到主线程

final Disposable[] disposable = {null};
        Observable.interval(0,1,TimeUnit.SECONDS,AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable[0] = d;
                    }

                    @Override
                    public void onNext(Long value) {
                        Log.i("result:",value + "");

                        btn.setText(time-- + "s");
                        if (value == 10){
                            disposable[0].dispose();
                            btn.setText("发送验证码");
                            time = 10;
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
复制代码
结果:

2018-11-29 17:52:56.043 6464-6464/com.example.qubin.demo I/result:: 0
2018-11-29 17:52:57.039 6464-6464/com.example.qubin.demo I/result:: 1
2018-11-29 17:52:58.040 6464-6464/com.example.qubin.demo I/result:: 2
2018-11-29 17:52:59.040 6464-6464/com.example.qubin.demo I/result:: 3
2018-11-29 17:53:00.037 6464-6464/com.example.qubin.demo I/result:: 4
2018-11-29 17:53:01.038 6464-6464/com.example.qubin.demo I/result:: 5
2018-11-29 17:53:02.037 6464-6464/com.example.qubin.demo I/result:: 6
2018-11-29 17:53:03.039 6464-6464/com.example.qubin.demo I/result:: 7
2018-11-29 17:53:04.039 6464-6464/com.example.qubin.demo I/result:: 8
2018-11-29 17:53:05.040 6464-6464/com.example.qubin.demo I/result:: 9
2018-11-29 17:53:06.038 6464-6464/com.example.qubin.demo I/result:: 10
复制代码
intervalRange

功能:范围之内轮询

注意:默认是在子线程启动,如果想更新UI操作则切换到主线程

Observable.intervalRange(1,10,0,1,TimeUnit.SECONDS,AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        Log.i("result:",aLong + "");
                        btn.setText(time-- + "s");
                        if (aLong == 10){
                            btn.setText("发送验证码");
                            time = 10;
                        }
                    }
                });
复制代码
结果:

2018-11-29 17:52:56.043 6464-6464/com.example.qubin.demo I/result:: 0
2018-11-29 17:52:57.039 6464-6464/com.example.qubin.demo I/result:: 1
2018-11-29 17:52:58.040 6464-6464/com.example.qubin.demo I/result:: 2
2018-11-29 17:52:59.040 6464-6464/com.example.qubin.demo I/result:: 3
2018-11-29 17:53:00.037 6464-6464/com.example.qubin.demo I/result:: 4
2018-11-29 17:53:01.038 6464-6464/com.example.qubin.demo I/result:: 5
2018-11-29 17:53:02.037 6464-6464/com.example.qubin.demo I/result:: 6
2018-11-29 17:53:03.039 6464-6464/com.example.qubin.demo I/result:: 7
2018-11-29 17:53:04.039 6464-6464/com.example.qubin.demo I/result:: 8
2018-11-29 17:53:05.040 6464-6464/com.example.qubin.demo I/result:: 9
2018-11-29 17:53:06.038 6464-6464/com.example.qubin.demo I/result:: 10
复制代码
range

功能:输出取值范围,参数为开始和结束范围,包含两者。

            Observable.range(1,10)
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.i("result:",integer + "");
                    }
                });
复制代码
结果:

2018-11-30 09:17:04.618 9682-9682/com.example.qubin.demo I/result:: 1
2018-11-30 09:17:04.618 9682-9682/com.example.qubin.demo I/result:: 2
2018-11-30 09:17:04.618 9682-9682/com.example.qubin.demo I/result:: 3
2018-11-30 09:17:04.618 9682-9682/com.example.qubin.demo I/result:: 4
2018-11-30 09:17:04.618 9682-9682/com.example.qubin.demo I/result:: 5
2018-11-30 09:17:04.618 9682-9682/com.example.qubin.demo I/result:: 6
2018-11-30 09:17:04.618 9682-9682/com.example.qubin.demo I/result:: 7
2018-11-30 09:17:04.618 9682-9682/com.example.qubin.demo I/result:: 8
2018-11-30 09:17:04.618 9682-9682/com.example.qubin.demo I/result:: 9
2018-11-30 09:17:04.618 9682-9682/com.example.qubin.demo I/result:: 10

复制代码
timer

功能:延时操作

参数一:延时时间

参数二:单位时间

Observable.timer(5,TimeUnit.SECONDS)
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        Log.i("result:",aLong + "");
                    }
                });
复制代码

startWith

功能:在被观察者发送数据之前添加一个额外的数据

            Observable.just(1,2,3,4)
                .startWith(5)
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.i("result:",integer + "");
                    }
                });
复制代码
结果:

2018-11-30 09:48:55.485 11293-11293/com.example.qubin.demo I/result:: 5
2018-11-30 09:48:55.485 11293-11293/com.example.qubin.demo I/result:: 1
2018-11-30 09:48:55.485 11293-11293/com.example.qubin.demo I/result:: 2
2018-11-30 09:48:55.485 11293-11293/com.example.qubin.demo I/result:: 3
2018-11-30 09:48:55.485 11293-11293/com.example.qubin.demo I/result:: 4

复制代码

在被观察者发送数据之前,我们插入了一个数字5。

startWithArray

功能:在被观察者发送数据之前添加多个额外的数据

            Observable.just(1,2,3,4,5)
                .startWithArray(5,6,7,8)
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.i("result:",integer + "");
                    }
                });
复制代码

当我们有多个数据需要插入到被观察者之前去的时候,用startWith是不可能的,因为只能插入一条数据。那么这个时候我们可以使用startWithArray里进行插入操作。

具体其他的一些操作符的用法,请参考我的github:RxJavaDemo

有兴趣可以关注我的小专栏,学习更多知识:小专栏

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值