RxJava2 操作符 map与 flatMap

(一) map就是变换需要操作的数据内容或者结构的意思

使用场景:

当原始数据不能满足我们的需求,但我们却需要依赖这一原始数据去获取满足我们需求的数据时,那么就用它

原理图如下:

举个栗子:


结果:


(二) flatmap

1.解决for循环嵌套

private static class Student{
    private int id;
    private String name;
    private String sex;
    private List<Course> courseList;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public List<Course> getCourseList() {
        return courseList;
    }

    public void setCourseList(List<Course> courseList) {
        this.courseList = courseList;
    }

    public static class Course{
        private int id;
        private String name;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
  //构建数据
    final List<Student> list = new ArrayList<>();
    for(int i = 0 ; i < 3 ; ++i){
        Student stu = new Student();
        stu.id = i;
        stu.name ="学生"+i;
        List<Student.Course> courses = new ArrayList<>();
        for(int j = 0 ; j < 4 ; ++j){
            Student.Course course = new Student.Course();
            course.setName(stu.name+"的课程"+j);
            course.setId(j);
            courses.add(course);
        }

        stu.setCourseList(courses);
        list.add(stu);
    }

/*创建observable将所有学生发送*/

    Observable.fromIterable(list)
        /*筛选学生:这里是学生id02*/
            .filter(new Predicate<Student>() {
                @Override
                public boolean test(Student student) throws Exception {
                    return student.id == 0 || student.id == 2;
                }
            })
       /*将学生的课程发送出去,从学生实例得到课程实例,再发射出去*/
            .flatMap(new Function<Student, ObservableSource<Student.Course>>() {
                @Override
                public ObservableSource<Student.Course> apply(Student student) throws Exception {
                    Log.d(TAG,"flatmap student name = "+student.name);
                    return Observable.fromIterable(student.getCourseList()).delay(10, TimeUnit.MILLISECONDS);
                }
            })
       /*得到课程再筛选id13的课程*/
            .filter(new Predicate<Student.Course>() {
                @Override
                public boolean test(Student.Course course) throws Exception {
                    return course.getId() == 1||course.id == 3;
                }
            })
       /*接受到学生的课程*/
            .subscribe(new Consumer<Student.Course>() {
                @Override
                public void accept(Student.Course course) throws Exception {
                    Log.d(TAG,"Consumer accept course = "+course.getName());
                }
            });

flatmap解决网络请求嵌套问题


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值