二、初识Rxjava操作符

just、from、map、flatMap操作符

  • Just — convert an object or a set of objects into an Observable that emits that or those objects
    (将一个对象或一组对象转换为可观察到的对象或对象 )
  • From — convert some other object or data structure into an Observable
    (接收一个集合作为输入,然后每次输出一个元素给subscriber)
  • Map — transform the items emitted by an Observable by applying a function to each item
    (就是用来把把一个事件转换为另一个事件的,用于变换Observable对象的,这样就可以实现链式调用)
  • FlatMap — transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable
    (接收一个Observable的输出作为输入,同时输出另外一个Observable)
static List studentList=new ArrayList(){
    {
        add(new Student("小明",26));
        add(new Student("小红",26));
    }
};

static Observable query(String name){
    Student student=new Student("刁",26);
    return Observable.just(student);
}
static Observable getAllStudent(Student student){
    return Observable.from(studentList);
}

  public RxJavaOperator() {
    Observable xiaoming=query("小明");
    Observable all=getAllStudent(null);

    Observer observer=new Observer() {
        @Override
        public void onCompleted() {
            Log.d(TAG, "onCompleted: ");
        }

        @Override
        public void onError(Throwable e) {
            Log.d(TAG, "onError: ");
        }

        @Override
        public void onNext(Student student) {
            Log.d(TAG, "onNext: "+student);
        }
    };
    xiaoming
  //             .flatMap(new Func1>() {
  //            @Override
  //            public Observable call(Student o) {
  //                return getAllStudent(o);
  //            }
  //        })
            .subscribe(observer);
}

输出:onNext: Student{name='刁', age=26}

//onNext: Student{name='小明', age=26}
//onNext: Student{name='小红, age=26}

你可能感兴趣的:(二、初识Rxjava操作符)