RxJava 2.0--- 合并操作符 Combining Operators

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() {
            @Override
            public String apply(Object[] objects) throws Exception {
                String msg = "";
                for (int i = 0;i () {
            @Override
            public void accept(String o) throws Exception {
                print("zip list accept:"+o);
            }
        });
    }
输出结果:
      zip accept:1 * 100=100
       zip accept:2 * 101=202
       zip accept:3 * 102=306
       zip accept:4 * 103=412
       zip accept:5 * 104=520
       zip accept:6 * 105=630
       zip accept:7 * 106=742
       zip accept:8 * 107=856
       zip accept:9 * 108=972
       zip accept:10 * 109=1090
       zip 4 accept:1 + 100 + 1 + 100 = 11001100
       zip list accept:1100110011001100
       zip 4 accept:2 + 101 + 2 + 101 = 21012101
       zip list accept:2101210121012101
       zip 4 accept:3 + 102 + 3 + 102 = 31023102
       zip list accept:3102310231023102
       zip 4 accept:4 + 103 + 4 + 103 = 41034103
       zip list accept:4103410341034103
       zip 4 accept:5 + 104 + 5 + 104 = 51045104
       zip list accept:5104510451045104
       zip 4 accept:6 + 105 + 6 + 105 = 61056105
       zip list accept:6105610561056105
       zip 4 accept:7 + 106 + 7 + 106 = 71067106
       zip list accept:7106710671067106
       zip 4 accept:8 + 107 + 8 + 107 = 81078107
       zip list accept:8107810781078107
       zip 4 accept:9 + 108 + 9 + 108 = 91089108
       zip list accept:9108910891089108
       zip 4 accept:10 + 109 + 10 + 109 = 1010910109
       zip list accept:10109101091010910109

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

你可能感兴趣的:(RxJava 2.0--- 合并操作符 Combining Operators)