rxjava2代码实战5--distinct,filter,buffer
来源:互联网 发布:老贾清弓 淘宝 编辑:程序博客网 时间:2024/06/17 03:23
distinct: 对数据源里的数据去重后输出
filter:根据过滤规则过滤数据
buffer:隔m个数取n个数
代码地址:
GitHub - GodisGod/Rxjava2Testhttps://github.com/GodisGod/Rxjava2Test
distinct:
我们这里有一个数据源:
private Integer[] datas = {1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5};
注意这里是Integer对象数组,如果是int[]是不行的哦,大家可以试一下
我们对这些数字去重后输出
Observable.fromArray(datas)//注意这里是Integer数组,如果是int[]是不行的哦,大家可以试一下 .distinct() .subscribe(new Consumer<Integer>() { @Override public void accept(Integer value) throws Exception { Log.i("LHD", "distinct : " + value); tvResult.append(value + " "); } });
结果:
再试一下把一个对象list过滤重复数据,这里需要我们自己定义一下根据对象的哪个字段来过滤,如果两个对象这个字段相等就过滤
我们定义了一个学生数据源:
Student student1 = new Student("LHD", 90); Student student2 = new Student("LHD1", 80); Student student3 = new Student("LHD2", 70); Student student4 = new Student("LHD3", 96); Student student2 = new Student("LHD4", 80); Student student22 = new Student("LHD4", 80); Student student23 = new Student("LHD4", 80); Student student3 = new Student("LHD5", 70); Student student4 = new Student("LHD5", 70);
Observable.fromIterable(students) .distinct(new Function<Student, String>() { @Override public String apply(Student student) throws Exception { return student.getName();//如果两个学生分数一样就过滤 } }) .subscribe(new Consumer<Student>() { @Override public void accept(Student student) throws Exception { Log.i("LHD", "distinct : " + student.getName()); tvResult.append(student.getName() + " "); } });
输出结果:
filter:使用很简单,大家看代码就会啦(#^.^#)
Observable.fromIterable(students) .filter(new Predicate<Student>() { @Override public boolean test(Student student) throws Exception { return student.getScore() > 80;//大于80分的过滤 } }).subscribe(new Consumer<Student>() { @Override public void accept(Student student) throws Exception { Log.i("LHD", " filter: " + student.getName()); tvResult.append("大于80分的:\n" + student.getName() + "\n"); } });
输出结果:
buffer:每隔m个数据输出n个数据
数据源:每三个里面输出前两个,如下图所示
Student student1 = new Student("LHD", 90);//-----------输出 Student student2 = new Student("LHD1", 80);//-----------输出 Student student3 = new Student("LHD2", 70); Student student4 = new Student("LHD3", 96);//-----------输出 Student student2 = new Student("LHD4", 80);//-----------输出 Student student22 = new Student("LHD4", 80); Student student23 = new Student("LHD4", 80);//-----------输出 Student student3 = new Student("LHD5", 70);//-----------输出 Student student4 = new Student("LHD5", 70);
Observable.fromIterable(students) .buffer(2, 3)//先取2个,每三个再取2个 .subscribe(new Consumer<List<Student>>() { @Override public void accept(List<Student> students) throws Exception { for (Student s : students) { Log.i("LHD", " buffer: " + s.getName()); tvResult.append(s.getName() + " " + s.getScore() + "\n"); } } });
输出结果:
阅读全文
0 0
- rxjava2代码实战5--distinct,filter,buffer
- rxjava2代码实战1--Map操作符
- rxjava2代码实战2--zip操作符
- rxjava2代码实战3--flatMap,concatMap操作符
- django model filter distinct
- RxJava2 / RxAndroid2操作符distinct去重
- RxJava2 / RxAndroid2的过滤器filter
- rxjava2代码实战4--interval,take操作符clipdrawable制作图片进度条
- RxJava2 实战知识梳理(1)
- SparkRDD学习之map ,flatmap,mappartitions,glom,union,cartesian,groupby,filter,distinct,subtract代码
- ReJava操作符 distinct 、Filter
- 35.List的map、flatMap、foreach、filter操作代码实战
- Protocol Buffer实战
- RxJava2.0的使用代码
- Distinct Values in Filter Dropdown(Distinct Sharpoint List Column Value)
- Scala深入浅出实战经典:35,List的map、flatMap、foreach、filter操作代码实战
- Rxjava2
- RXJava2
- 修改WAMP的mysql数据库密码并配置phpMyadmin
- UIView动画类型
- 恢复 git reset --hard *删除的文件
- 使用Serv_U架设FTP服务器(图解)
- 剑指offer—数字在排序数组中出现的次数
- rxjava2代码实战5--distinct,filter,buffer
- 机器学习入门:线性回归及梯度下降
- iOS --点击事件分发机制
- 【笔记-C语言】 字符串
- pintos (5) --Advanced Scheduler
- Dataland CD Label Designer(光盘封面制作软件)官方注册版V7.1.754下载
- 1204: 鸡兔同笼
- ST表
- java多线程(1)_正确使用Condition实现等待通知