rxjava2代码实战5--distinct,filter,buffer

时间:2021-01-01 18:26:20

distinct: 对数据源里的数据去重后输出
filter:根据过滤规则过滤数据
buffer:隔m个数取n个数

代码地址:

GitHub - GodisGod/Rxjava2Test
https://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 + " ");
                                    }
                                });

结果:
rxjava2代码实战5--distinct,filter,buffer

再试一下把一个对象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() + " ");
                            }
                        });

输出结果:

rxjava2代码实战5--distinct,filter,buffer

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");
                    }
                });

输出结果:

rxjava2代码实战5--distinct,filter,buffer

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");
                                }

                            }
                        });

输出结果:

rxjava2代码实战5--distinct,filter,buffer