RxJava 变换操作符 map / flatmap
Map
拦截与接收被观察者发送的事件,按照需求改变事件,再发送给观察者「观察者接收到的事件是改变后的事件」
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
emitter.onNext(1);
emitter.onNext(2);
emitter.onNext(3);
}
}).map(new Function<Integer, String>() {
@Override
public String apply(Integer integer) throws Exception {
return "This is result " + integer;
}
}).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.d(TAG, s); //接收的String为改变后的String
}
});
//Print
D/TAG: This is result 1
D/TAG: This is result 2
D/TAG: This is result 3
网友评论