- map()
/** * map被观察者转换器,可以将一个被观察者(Observable)发送的数据依次转换为需要的数据,可以执行多个map * 来完成功能 */ public void map(View view) { Observable.create(new ObservableOnSubscribe<Integer>() { @Override public void subscribe(ObservableEmitter<Integer> e) throws Exception { for (int i = 1; i <= 3; i++) { e.onNext(i); } e.onComplete(); } }).map(new Function<Integer, String>() { @Override public String apply(Integer integer) throws Exception { return "\n" + integer + "桌麻婆豆腐开始准备:\n"; } }).map(new Function<String, String>() { @Override public String apply(String data) throws Exception { return data + " 第一步:豆腐切块下如盐水水抄水一分钟,牛肉切沫\n"; } }).map(new Function<String, String>() { @Override public String apply(String data) throws Exception { return data + " 第二步:准备小葱切段,豆豉,刀口辣椒(可以用花椒面和辣椒面代替),泡椒,生姜等\n"; } }).map(new Function<String, String>() { @Override public String apply(String data) throws Exception { return data + " 第三步:牛肉沫爆香,放入豆瓣,以及第二部准备好得配料然后下入豆腐,倒入高汤\n"; } }).map(new Function<String, String>() { @Override public String apply(String data) throws Exception { return data + " 第四步:分为三步勾芡,装盘\n"; } }).map(new Function<String, String>() { @Override public String apply(String data) throws Exception { return data + " 第五步:放入葱段,,花椒面等,上菜\n"; } }).subscribeOn(Schedulers.io()) .unsubscribeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<String>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(String value) { Log.e("map", value); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); }
- flatMap可以将一个数据类型转换成另一个类型然后发射到观察者
public void flatMap(View view) { Observable.create(new ObservableOnSubscribe<Integer>() { @Override public void subscribe(ObservableEmitter<Integer> e) throws Exception { for (int i = 1; i <= 3; i++) { e.onNext(i); } e.onComplete(); } }).flatMap(new Function<Integer, ObservableSource<List<String>>>() { @Override public ObservableSource<List<String>> apply(Integer integer) throws Exception { Log.e("flatMap", "integer:" + integer); return flatMapItem(); } }) .subscribeOn(Schedulers.io()) .unsubscribeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<List<String>>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(List<String> value) { Log.e("flatMap", value.toString()); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } public Observable<List<String>> flatMapItem() { return Observable.create(new ObservableOnSubscribe<List<String>>() { @Override public void subscribe(ObservableEmitter<List<String>> e) throws Exception { List<String> list = new ArrayList<>(); list.add(String.valueOf(0)); list.add(String.valueOf(1)); e.onNext(list); // e.onComplete(); } }); }
- concatMap
public void concatMap(View view) { Observable.create(new ObservableOnSubscribe<String>() { @Override public void subscribe(ObservableEmitter<String> e) throws Exception { for (int i = 1; i <= 3; i++) { e.onNext(i + ""); } e.onComplete(); } }).concatMap(new Function<String, ObservableSource<String>>() { @Override public ObservableSource<String> apply(String s) throws Exception { List<String> list = new ArrayList(); for (int i = 1; i < 3; i++) { list.add("concatMap:" + i); } return Observable.fromIterable(list); } }).subscribeOn(Schedulers.io()) .unsubscribeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<String>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(String value) { LogUtils.toE("concatMap", value); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); }
-
RxJava操作符—变换操作符
最新推荐文章于 2024-04-22 02:22:42 发布