📜  RxJava-条件运算符

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


以下是评估一个或多个可观察物或发射项目的运算符。

Sr.No. Operator & Description
1

All

Evaluates all items emitted to meet given criteria.

2

Amb

Emits all items from the first Observable only given multiple Observables.

3

Contains

Checks if an Observable emits a particular item or not.

4

DefaultIfEmpty

Emits default item if Observable do not emit anything.

5

SequenceEqual

Checks if two Observables emit the same sequence of items.

6

SkipUntil

Discards items emitted by first Observable until a second Observable emits an item.

7

SkipWhile

Discard items emitted by an Observable until a given condition becomes false.

8

TakeUntil

Discards items emitted by an Observable after a second Observable emits an item or terminates.

9

TakeWhile

Discard items emitted by an Observable after a specified condition becomes false.

条件运算符示例

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

ObservableTester.java

import io.reactivex.Observable;
//Using defaultIfEmpty operator to operate on an Observable
public class ObservableTester  {
   public static void main(String[] args) {    
      final StringBuilder result = new StringBuilder();
      Observable.empty()
      .defaultIfEmpty("No Data")
      .subscribe(s -> result.append(s));
      System.out.println(result);
      String[] letters = {"a", "b", "c", "d", "e", "f", "g"};
      final StringBuilder result1 = new StringBuilder();
      Observable.fromArray(letters)
      .firstElement()
      .defaultIfEmpty("No data")   
      .subscribe(s -> result1.append(s));
      System.out.println(result1);
   }
}

验证结果

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

C:\RxJava>javac ObservableTester.java

现在如下运行ObservableTester-

C:\RxJava>java ObservableTester

它应该产生以下输出-

No Data
a