1、map()操作符(一对一的转化)
Observable.just("Hello")
.map(new Func1<String, String>() {
@Override
public String call(String s) {
return s + "rxjava";
}
}).subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.d("s", s);
}
});
lambda表达式
Observable
.just("hello")
.map(s -> s+"rxjava")
.subscribe(s -> Log.d("s",s));
2、flapMap():一对多
Student[] students = ...;
Subscriber<Course> subscriber = new Subscriber<Course>() {
@Override
public void onNext(Course course) {
Log.d(tag, course.getName());
}
...
};
Observable.from(students)
.flatMap(new Func1<Student, Observable<Course>>() {
@Override
public Observable<Course> call(Student student) {
return Observable.from(student.getCourses());
}
})
.subscribe(subscriber);