JAVA语言操作符学习之过滤操作符
小标 2018-12-19 来源 : 阅读 1072 评论 0

摘要:本文主要向大家介绍了JAVA语言操作符学习之过滤操作符,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言操作符学习之过滤操作符,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。


一、简介:前面几篇已经介绍了关于RxJAva的创建、转换、功能等操作符,接下来本篇一起看看剩下的条件过滤的操作符


二、过滤操作符的作用:


过滤 / 筛选 被观察者(Observable)发送的事件 & 观察者 (Observer)接收的事件


三、过滤操作符的分类




四、操作符介绍


filter()


作用:根据条件过滤发送的事件,符合条件的才会发送参数:Predicate 对象 当返回true时,事件才会分送,若返回false则忽略此次发送使用示例


Observable.just(1, 2, 3,4,5)

             .filter(new Predicate<integer>() {

                 @Override

                 public boolean test(Integer integer) throws Exception {

                     return integer <= 3; // 此处为判断条件返回true则发送事件

                 }

             })

             .subscribeOn(Schedulers.io())

             .observeOn(AndroidSchedulers.mainThread())

             .subscribe(new Consumer() {

                 @Override

                 public void accept(Object o) throws Exception {

                     Log.e("=====", o + "");

                 }

             });</integer>

   



输出结果:


05-06 11:13:32.250 15316-15316/com.example.administrator.googleplay E/=====: 1

05-06 11:13:32.250 15316-15316/com.example.administrator.googleplay E/=====: 2

05-06 11:13:32.250 15316-15316/com.example.administrator.googleplay E/=====: 3

   



offType ()


作用根据发送数据的类型过滤参数: 字节码 文件 .class 表示允许输出的数据类型使用示例


Observable.just(1, "A", 3,"C",5)

                .ofType(Integer.class) 

                .subscribeOn(Schedulers.io())

                .observeOn(AndroidSchedulers.mainThread())

                .subscribe(new Consumer() {

                    @Override

                    public void accept(Object o) throws Exception {

                        Log.e("=====", o + "");

                    }

                });

   



输出结果:


05-06 11:18:40.943 15316-15316/com.example.administrator.googleplay E/=====: 1

05-06 11:18:40.944 15316-15316/com.example.administrator.googleplay E/=====: 3

05-06 11:18:40.944 15316-15316/com.example.administrator.googleplay E/=====: 5

   



输出结果均为Integer类型,String类型的数据被过滤掉了


skip()/ skipLast()


作用:跳过一定数量的事件再发送参数:integer 表示跳过的个数重载


//参数: time 跳过的时间、 TimeUnit 时间单位

skip(long time, TimeUnit unit)

//参数: time 跳过的时间、 TimeUnit 时间单位 、Scheduler  调度器

skip(long time, TimeUnit unit, Scheduler scheduler)

   



4、使用示例


Observable.just(1, "A", 3,"C",5)

               //表述跳过的个数

               .skip(2)

               .subscribeOn(Schedulers.io())

               .observeOn(AndroidSchedulers.mainThread())

               .subscribe(new Consumer() {

                   @Override

                   public void accept(Object o) throws Exception {

                       Log.e("=====", o + "");

                   }

               });

   



输出结果:


05-06 11:27:39.211 15316-15316/com.example.administrator.googleplay E/=====: 3

05-06 11:27:39.211 15316-15316/com.example.administrator.googleplay E/=====: C

05-06 11:27:39.211 15316-15316/com.example.administrator.googleplay E/=====: 5

   



5、skip 重载和 skipLast 的使用示例


Observable.intervalRange(0,7,0,1,TimeUnit.SECONDS)

                .skip(2,TimeUnit.SECONDS) //跳过前两秒的事件

                .skipLast(2)//跳过最后两个数据

                .subscribeOn(Schedulers.io())

                .observeOn(AndroidSchedulers.mainThread())

                .subscribe(new Consumer() {

                    @Override

                    public void accept(Object o) throws Exception {

                        Log.e("=====", o + "");

                    }

                });

   



输出结果:


05-06 11:31:36.040 15316-15316/com.example.administrator.googleplay E/=====: 2

05-06 11:31:37.040 15316-15316/com.example.administrator.googleplay E/=====: 3

05-06 11:31:38.038 15316-15316/com.example.administrator.googleplay E/=====: 4

   



从0开始输出7个数 即0——6,过滤前两秒数据0和1,提过最后两个数据5和6,输出之间的数据2,3,4;


distinct() / distinctUntilChanged()


作用:对发送事件中重复的数据进行过滤distinct() 使用示例


Observable.just(1,2,4,3,3,4,4,5,5,5)

              .distinct()

              .subscribe(new Consumer<integer>() {

                  @Override

                  public void accept(Integer integer) throws Exception {

                      Log.e("=====", integer + "");

                  }

              });</integer>

   



输出结果:


05-06 11:41:41.712 16360-16360/com.example.administrator.googleplay E/=====: 1

05-06 11:41:41.712 16360-16360/com.example.administrator.googleplay E/=====: 2

05-06 <code class="j    

   


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved