RxJAVA 常用操作符

时间:2020-12-01 17:48:56

 1 zip() 是合并多个Observable 发射出来的数据,并且根据FUNC2 函数变换产生一个新的值

RxJAVA 常用操作符

这里会把 1A 2B ,如果一个序列介绍了,就不会产生新的值了;

 

 List<String> list = new ArrayList();
for (int i =0;i<=20;i++){
list.add(
"arg="+i+"====>>");
}
Observable
<Long> observable = Observable.interval(1, TimeUnit.SECONDS);
Observable
<String> observable1 = Observable.from(list);
Observable.zip(observable, observable1,
new Func2<Long, String, Object>() {
@Override
public Object call(Long aLong, String s) {
return s+aLong;
}
}).subscribe(
new Action1<Object>() {
@Override
public void call(Object o) {
Log.i(
"===>>",(String) o);
}
});

结果为

01-18 15:25:33.042 1498-1694/sean.com.test I/===>>: arg=16====>>16
01-18 15:25:34.042 1498-1694/sean.com.test I/===>>: arg=17====>>17
01-18 15:25:35.042 1498-1694/sean.com.test I/===>>: arg=18====>>18
01-18 15:25:36.041 1498-1694/sean.com.test I/===>>: arg=19====>>19
01-18 15:25:37.042 1498-1694/sean.com.test I/===>>: arg=20====>>20

 

2  Marge() 是把多个Abservable里面的值合并成一个

RxJAVA 常用操作符

最后的序列如上图所示

 List<Long> list = new ArrayList();
for (int i =0;i<20;i++){
list.add(Long.valueOf(i));
}
List
<Long> list1 = new ArrayList();
for (int i =20;i<40;i++){
list1.add(Long.valueOf(i));
}
Observable
<Long> observable = Observable.from(list);
Observable
<Long> observable1 = Observable.from(list1);
Observable.merge(observable, observable1).subscribe(
new Action1<Object>() {
@Override
public void call(Object o) {
Log.i(
"===>>", o+"");
}
});

结果为 

01-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 0
01-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 1
01-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 2
01-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 3
01-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 4
01-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 5
01-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 6
01-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 7
01-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 8
01-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 9
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 10
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 11
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 12
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 13
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 14
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 15
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 16
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 17
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 18
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 19
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 20
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 21
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 22
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 23
01-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 24
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 25
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 26
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 27
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 28
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 29
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 30
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 31
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 32
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 33
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 34
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 35
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 36
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 37
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 38
01-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 39

 

3 Flatmap 和 map, Flatmap 返回一个新的Observable, 而map返回原来的Observerable

     List<Long> list = new ArrayList();
for (int i =0;i<20;i++){
list.add(Long.valueOf(i));
}
Observable
<Long> observable = Observable.from(list);
observable.flatMap(
new Func1<Long, Observable<String>>() {
@Override
public Observable<String> call(Long aLong) {
return Observable.just("hello"+aLong);
}
}).subscribe(
new Action1<String>() {
@Override
public void call(String s) {
Log.i(
"===>>",s);
}
});
}

 

4 Concat 链接多个Observable(不能用不定参数??)

RxJAVA 常用操作符