1、map
map 操作符的作用是对 对象进行转换
Observable.just(1)
.map(new Function<Integer, String>() {
@Override
public String apply(Integer s) throws Exception {
return "My Name is" + s;
}
}).subscribe(consumer);
2、flatMap
flatMap 操作符的作用是对 一组对象进行转换
Observable.fromArray(1, 2, 3).flatMap(new Function<Integer, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(Integer integer) throws Exception {
if (2 == integer) {
return Observable.just("My Name is:" + integer);
}
return Observable.just("My Name is:" + integer).delay(200, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread());
}
}).subscribe(consumer);
Toast 2、1、3
3、concatMap
Observable.fromArray(1, 2, 3).concatMap(new Function<Integer, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(Integer integer) throws Exception {
if (2 == integer) {
return Observable.just("My Name is:" + integer);
}
return Observable.just("My Name is:" + integer).delay(200, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread());
}
}).subscribe(consumer);
Toast =1、2、3
concatMap能保证顺序,flatMap 不能保证顺序
4、cast
cast操作符,将类对象进行转换
Object[] objectsArr = {"1", "2", "3"};
Observable.from(objectsArr).cast(String.class).subscribe(onNextAction);
5、buffer
buffer将原有Observable转换为一个新的Observable,这个新的Observable每次发送一组值,而不是一个个进行发送
Observable.just(1, 2, 3, 4, 5)
.buffer(3,1).subscribe(new Consumer<List<Integer>>() {
@Override
public void accept(List<Integer> list) throws Exception {
for (Integer i : list) {
Toast.makeText(getActivity(), "new Number i is:" + i, Toast.LENGTH_SHORT).show();
Log.e("tag","new Number i is:" + i);
}
Toast.makeText(getActivity(), "Another request is called", Toast.LENGTH_SHORT).show();
Log.e("tag","Another request is called");
}
});
tag: new Number i is:1
tag: new Number i is:2
tag: new Number i is:3
tag: Another request is called
tag: new Number i is:2
tag: new Number i is:3
tag: new Number i is:4
tag: Another request is called
tag: new Number i is:3
tag: new Number i is:4
tag: new Number i is:5
tag: Another request is called
tag: new Number i is:4
tag: new Number i is:5
tag: Another request is called
tag: new Number i is:5
tag: Another request is called
Observable.just(1, 2, 3, 4, 5)
.buffer(3).subscribe(new Consumer<List<Integer>>() {
@Override
public void accept(List<Integer> list) throws Exception {
for (Integer i : list) {
Toast.makeText(getActivity(), "new Number i is:" + i, Toast.LENGTH_SHORT).show();
Log.e("tag","new Number i is:" + i);
}
Toast.makeText(getActivity(), "Another request is called", Toast.LENGTH_SHORT).show();
Log.e("tag","Another request is called");
}
});
tag: new Number i is:1
tag: new Number i is:2
tag: new Number i is:3
tag: Another request is called
tag: new Number i is:4
tag: new Number i is:5
tag: Another request is called