📜  RxJava-合并运算符

📅  最后修改于: 2020-11-12 05:29:05             🧑  作者: Mango


以下是用于从多个Observable创建单个Observable的运算符。

Sr.No. Operator & Description
1 And/Then/When

Combine item sets using Pattern and Plan intermediaries.

2 CombineLatest

Combine the latest item emitted by each Observable via a specified function and emit resulted item.

3 Join

Combine items emitted by two Observables if emitted during time-frame of second Observable emitted item.

4 Merge

Combines the items emitted of Observables.

5 StartWith

Emit a specified sequence of items before starting to emit the items from the source Observable

6 Switch

Emits the most recent items emitted by Observables.

7 Zip

Combines items of Observables based on function and emits the resulted items.

合并算子示例

使用您选择的任何编辑器(例如,C:\> RxJava)创建以下Java程序。

ObservableTester.java

import io.reactivex.Observable;
//Using combineLatest operator to combine Observables
public class ObservableTester {
   public static void main(String[] args) {    
      Integer[] numbers = { 1, 2, 3, 4, 5, 6};
      String[] letters = {"a", "b", "c", "d", "e", "f", "g"};
      final StringBuilder result = new StringBuilder();
      Observable observable1 = Observable.fromArray(letters);
      Observable observable2 = Observable.fromArray(numbers);
      Observable.combineLatest(observable1, observable2, (a,b) -> a + b)
         .subscribe( letter -> result.append(letter));
      System.out.println(result);
   }
}

验证结果

使用javac编译器编译类,如下所示:

C:\RxJava>javac ObservableTester.java

现在如下运行ObservableTester-

C:\RxJava>java ObservableTester

它应该产生以下输出-

g1g2g3g4g5g6