📜  RxJava-创建运算符

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


以下是用于创建Observable的运算符。

Sr.No. Operator & Description
1

Create

Creates an Observable from scratch and allows observer method to call programmatically.

2

Defer

Do not create an Observable until an observer subscribes. Creates a fresh observable for each observer.

3

Empty/Never/Throw

Creates an Observable with limited behavior.

4

From

Converts an object/data structure into an Observable.

5

Interval

Creates an Observable emitting integers in sequence with a gap of specified time interval.

6

Just

Converts an object/data structure into an Observable to emit the same or same type of objects.

7

Range

Creates an Observable emitting integers in sequence of given range.

8

Repeat

Creates an Observable emitting integers in sequence repeatedly.

9

Start

Creates an Observable to emit the return value of a function.

10

Timer

Creates an Observable to emit a single item after given delay.

创建运算符示例

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

ObservableTester.java

import io.reactivex.Observable;
//Using fromArray operator to create an Observable
public class ObservableTester  {
   public static void main(String[] args) { 
      String[] letters = {"a", "b", "c", "d", "e", "f", "g"};
      final StringBuilder result = new StringBuilder();
      Observable observable = Observable.fromArray(letters);
      observable
         .map(String::toUpperCase)
         .subscribe( letter -> result.append(letter));
      System.out.println(result);
   }
}

验证结果

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

C:\RxJava>javac ObservableTester.java

现在如下运行ObservableTester-

C:\RxJava>java ObservableTester

它应该产生以下输出-

ABCDEFG