变换操作符即为中事件发送和接收之间,对数据进行修改类型变换等操作
如图:
1,map操作符的使用如下
Observable.just(1,2,3)
.map(new Function<Integer, String>() {
@Override
public String apply(Integer integer) throws Exception {
return integer+" is to String num";
}
})
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
Log.d("MainActivity","onNext:"+s);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
如上在观察者中的onNext方法中接收的都是map操作符中转换之后的String文本字符串
注:Map操作符可以使用多次增加,类型可以自由变换如下:
Observable.just(1,2,3)
.map(new Function<Integer, String>() {
@Override
public String apply(Integer integer) throws Exception {
return integer+" is to String num";
}
})
.map(new Function<String, String>() {
@Override
public String apply(String s) throws Exception {
return s+" 第二次操作符的添加";
}
})
.subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.d("MainActivity","onNext:"+s);
}
});
2,flatMap操作符的使用
Observable.just(111,222,333) //上游
//变换操作符
.flatMap(new Function<Integer, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(final Integer integer) throws Exception {
//ObservableSource 可以再次手动发送事件
return Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
emitter.onNext(integer+"flatMap 变换");
emitter.onNext(integer+"flatMap 变换");
emitter.onNext(integer+"flatMap 变换");
}
});
}
}).subscribe(new Consumer<String>() {//下游
@Override
public void accept(String s) throws Exception {
Log.d("MainActivity","accept:"+s);
}
});
flatMap操作符转换会后,会生成ObservableSource,于是可以从新修改变换后,再次发送。
注:flatMap是不排序的
3,concatMap操作符的使用
注:concatMap操作符是排序的
Observable.just("A","B","C")
.concatMap(new Function<String, ObservableSource<?>>() {
@Override
public ObservableSource<?> apply(String s) throws Exception {
List<String> list=new ArrayList<>();
for (int i = 0; i <3 ; i++) {
list.add("item:"+i);
}
return Observable.fromIterable(list).delay(5,TimeUnit.SECONDS);
}
})
.subscribe(new Consumer<Object>() {
@Override
public void accept(Object s) throws Exception {
Log.d("MainActivity","accept:"+s);
}
});
4,groupBy操作符的使用
Observable.just(1000,2000,300,400)
.groupBy(new Function<Integer, String>() {
@Override
public String apply(Integer integer) throws Exception {
return integer>1000?"高配置":"低配置";
}
}).subscribe(new Consumer<GroupedObservable<String, Integer>>() {
@Override
public void accept(GroupedObservable<String, Integer> stringIntegerGroupedObservable) throws Exception {
Log.d("MainActivity","accept:"+stringIntegerGroupedObservable.getKey());
// 输出细节还需要再包裹一层
stringIntegerGroupedObservable.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.d("MainActivity","accept:"+integer);
}
});
}
});
groupBy操作符,适用于对数据进行分类记录操作
5,buffer操作符的使用
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
for (int i = 0; i <100 ; i++) {
emitter.onNext(i);
}
emitter.onComplete();
}
})
.buffer(20)
.subscribe(new Consumer<List<Integer>>() {
@Override
public void accept(List<Integer> integers) throws Exception {
Log.d("MainActivity","accept:"+integers);
}
});
如上输出结果如下:
相当于按照数据个数进行分组
总结: