4.合并操作符
将多个Observalbes合并成一个Observable
Operators that work with multiple source Observables to create a single Observable
● Join
● Merge
● SwitchMap
● Zip
● withLatestFrom
● combineLatest
Join
将两种基于重叠时间的观察值所产生的项关联起来。当从一个或两个源观察到的多个项目重叠时,这些项目组合在一起的顺序是没有保证的。
public Observable getFirstObservable(){
return Observable.intervalRange(1,10,0,0, TimeUnit.MILLISECONDS);
};
public Observable getSecondObservable(){
return Observable.intervalRange(100,10,0,0, TimeUnit.MILLISECONDS);
};
getFirstObservable()
.join(getSecondObservable(), new Function>() {
@Override
public ObservableSource apply(Long aLong) throws Exception {
return Observable.just(aLong *2);
}
}, new Function>() {
@Override
public ObservableSource apply(Long aLong) throws Exception {
return Observable.just(aLong *2);
}
},new BiFunction() {
@Override
public String apply(Long aLong, Long aLong2) throws Exception {
return aLong +"+"+aLong2+"="+(aLong+aLong2);
}
}).subscribeOn(Schedulers.computation())
.subscribe(new Consumer() {
@Override
public void accept(String s) throws Exception {
print("join accept:"+s);
}
});
输出结果:
join accept:1+100=101
join accept:2+100=102
join accept:3+100=103
join accept:4+100=104
join accept:5+100=105
join accept:6+100=106
join accept:7+100=107
join accept:8+100=108
join accept:9+100=109
join accept:10+100=110
Merge
merge操作符将两个甚至更多的Observables合并到一个发射的数据项里, 没有任何转换 。
Observable.just(11).merge(getFirstObservable(),getSecondObservable()).subscribe(new Consumer() {
@Override
public void accept(Long s) throws Exception {
print("merge accept:"+s);//1,2,...10,100,101,...,109
}
});
getFirstObservable()
.mergeWith(getSecondObservable()).subscribe(new Consumer() {
@Override
public void accept(Long s) throws Exception {
print("mergeWith accept:"+s);//1,2,...10,100,101,...,109
}
});
Observable.concat(getFirstObservable(),getSecondObservable()).subscribe(new Consumer() {
@Override
public void accept(Long aLong) throws Exception {
print("concat accept"+aLong);//1,2,...10,100,101,...,109
}
});
SwitchMap
它会释放出从应用到源观察源所发出的最近发射的项
getFirstObservable().switchMap(new Function>() {
@Override
public ObservableSource apply(Long s) throws Exception {
return Observable.just(s+"*="+s*s);
}
}).subscribe(new Consumer() {
@Override
public void accept(String s) throws Exception {
print("switchMap accept:"+s);// 1,4,9,16,25,36,49,64,81,100
}
});
Zip
zip操作符合并两个或者多个Observables发射出的数据项,根据指定的函数Function变换它们,并发射一个新值。
Observable.zip(getFirstObservable(), getSecondObservable(), new BiFunction() {
@Override
public String apply(Long aLong, Long aLong2) throws Exception {
return aLong+" * "+aLong2 +"="+aLong *aLong2;
}
}).subscribe(new Consumer() {
@Override
public void accept(String s) throws Exception {
print("zip accept:"+s);
}
});
Observable.zip(getFirstObservable(), getSecondObservable(), getFirstObservable(), getSecondObservable(), new Function4() {
@Override
public String apply(Long aLong, Long aLong2, Long aLong3, Long aLong4) throws Exception {
return aLong+" + "+aLong2+" + "+aLong3+" + "+aLong4 +" = "+aLong +aLong2 +aLong3 +aLong4;
}
}).subscribe(new Consumer() {
@Override
public void accept(String s) throws Exception {
print("zip 4 accept:"+s);
}
});
List> list = new ArrayList<>();
list.add(getFirstObservable());
list.add(getSecondObservable());
list.add(getFirstObservable());
list.add(getSecondObservable());
list.add(getFirstObservable());
list.add(getSecondObservable());
list.add(getFirstObservable());
list.add(getSecondObservable());
Observable.zip(list, new Function
withLatestFrom
只有当源观察源序列发出一个项目时,才可以使用该函数将指定的观测源合并到这个可见的资源中。
getFirstObservable()
.withLatestFrom(getSecondObservable(), new BiFunction() {
@Override
public String apply(Long aLong, Long aLong2) throws Exception {
return aLong +"+"+aLong2+"="+(aLong+aLong2);
}
}).subscribe(new Consumer() {
@Override
public void accept(String s) throws Exception {
print("withLatestFrom accept:"+s);
}
});
输出结果:
withLatestFrom accept:4+109=113
withLatestFrom accept:5+109=114
withLatestFrom accept:6+109=115
withLatestFrom accept:7+109=116
withLatestFrom accept:8+109=117
withLatestFrom accept:9+109=118
withLatestFrom accept:10+109=119
combineLatest
combineLatest操作符有点类似zip操作符的特殊形式,用于将最近发射数据。 通过给定的聚合函数将源观察到的项目组合起来的结果发射出去
Observable.combineLatest(getFirstObservable(), getSecondObservable(), new BiFunction() {
@Override
public String apply(Long aLong, Long aLong2) throws Exception {
return aLong +" * "+aLong2 +" = "+ aLong * aLong2;
}
}).subscribe(new Consumer() {
@Override
public void accept(String s) throws Exception {
print("combineLatest accept:"+s);
}
});
输出结果:
combineLatest accept:10 * 100 = 1000
combineLatest accept:10 * 101 = 1010
combineLatest accept:10 * 102 = 1020
combineLatest accept:10 * 103 = 1030
combineLatest accept:10 * 104 = 1040
combineLatest accept:10 * 105 = 1050
combineLatest accept:10 * 106 = 1060
combineLatest accept:10 * 107 = 1070
combineLatest accept:10 * 108 = 1080
combineLatest accept:10 * 109 = 1090