|
1 | 1 | This section explains operators with which you conditionally emit or transform Observables, or can do boolean evaluations of them:
|
2 | 2 |
|
3 | 3 | ### Conditional Operators
|
4 |
| -* [**`amb( )`**](http://reactivex.io/documentation/operators/amb.html) — given two or more source Observables, emits all of the items from the first of these Observables to emit an item |
5 |
| -* [**`defaultIfEmpty( )`**](http://reactivex.io/documentation/operators/defaultifempty.html) — emit items from the source Observable, or emit a default item if the source Observable completes after emitting no items |
6 |
| -* (`rxjava-computation-expressions`) [**`doWhile( )`**](http://reactivex.io/documentation/operators/repeat.html) — emit the source Observable's sequence, and then repeat the sequence as long as a condition remains true |
7 |
| -* (`rxjava-computation-expressions`) [**`ifThen( )`**](http://reactivex.io/documentation/operators/defer.html) — only emit the source Observable's sequence if a condition is true, otherwise emit an empty or default sequence |
8 |
| -* [**`skipUntil( )`**](http://reactivex.io/documentation/operators/skipuntil.html) — discard items emitted by a source Observable until a second Observable emits an item, then emit the remainder of the source Observable's items |
9 |
| -* [**`skipWhile( )`**](http://reactivex.io/documentation/operators/skipwhile.html) — discard items emitted by an Observable until a specified condition is false, then emit the remainder |
10 |
| -* (`rxjava-computation-expressions`) [**`switchCase( )`**](http://reactivex.io/documentation/operators/defer.html) — emit the sequence from a particular Observable based on the results of an evaluation |
11 |
| -* [**`takeUntil( )`**](http://reactivex.io/documentation/operators/takeuntil.html) — emits the items from the source Observable until a second Observable emits an item or issues a notification |
12 |
| -* [**`takeWhile( )` and `takeWhileWithIndex( )`**](http://reactivex.io/documentation/operators/takewhile.html) — emit items emitted by an Observable as long as a specified condition is true, then skip the remainder |
13 |
| -* (`rxjava-computation-expressions`) [**`whileDo( )`**](http://reactivex.io/documentation/operators/repeat.html) — if a condition is true, emit the source Observable's sequence and then repeat the sequence as long as the condition remains true |
14 |
| - |
15 |
| -> (`rxjava-computation-expressions`) — indicates that this operator is currently part of the optional `rxjava-computation-expressions` package under `rxjava-contrib` and is not included with the standard RxJava set of operators |
| 4 | + |
| 5 | +### Outline |
| 6 | + |
| 7 | +- [`amb`](#all) |
| 8 | +- [`defaultIfEmpty`](#defaultIfEmpty) |
| 9 | +- [`skipUntil`](#skipUntil) |
| 10 | +- [`skipWhile`](#skipWhile) |
| 11 | +- [`takeUntil`](#takeUntil) |
| 12 | +- [`takeWhile`](#takeUntil) |
| 13 | + |
| 14 | +## amb |
| 15 | + |
| 16 | +**Available in:**  `Flowable`,  `Observable`,  `Maybe`,  `Single`,  `Completable` |
| 17 | + |
| 18 | +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/amb.html](http://reactivex.io/documentation/operators/amb.html) |
| 19 | + |
| 20 | +given two or more source Observables, emits all of the items from the first of these Observables to emit an item |
| 21 | + |
| 22 | +```java |
| 23 | + Observable source1 = Observable.range(1, 5); |
| 24 | + Observable source2 = Observable.range(6, 5); |
| 25 | + Observable.amb(new ArrayList(Arrays.asList(source1, source2))) |
| 26 | + .subscribe(next -> System.out.printf("next: %s\n", next), // onNext |
| 27 | + throwable -> System.out.printf("error: %s\n", throwable), //onError |
| 28 | + () -> System.out.println("Completed") //onComplete |
| 29 | + ); |
| 30 | +``` |
| 31 | +## defaultIfEmpty |
| 32 | + |
| 33 | +**Available in:**  `Flowable`,  `Observable`,  `Maybe`,  `Single`,  `Completable` |
| 34 | + |
| 35 | +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/defaultifempty.html](http://reactivex.io/documentation/operators/defaultifempty.html) |
| 36 | + |
| 37 | +emit items from the source Observable, or emit a default item if the source Observable completes after emitting no items |
| 38 | + |
| 39 | +```java |
| 40 | + Observable.empty().defaultIfEmpty(1).blockingSubscribe(next -> System.out.printf("next: %s\n", next), // onNext |
| 41 | + throwable -> System.out.printf("error: %s", throwable), //onError |
| 42 | + () -> System.out.println("Completed") //onComplete |
| 43 | + ); |
| 44 | +``` |
| 45 | + |
| 46 | +## skipUntil |
| 47 | + |
| 48 | +**Available in:**  `Flowable`,  `Observable`,  `Maybe`,  `Single`,  `Completable` |
| 49 | + |
| 50 | +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/skipuntil.html](http://reactivex.io/documentation/operators/skipuntil.html) |
| 51 | + |
| 52 | +discard items emitted by a source Observable until a second Observable emits an item, then emit the remainder of the source Observable's items |
| 53 | + |
| 54 | +```java |
| 55 | +Observable observable1 = Observable.range(1, 10).doOnNext(next -> Thread.sleep(1000)); |
| 56 | + |
| 57 | +observable1.skipUntil(Observable.timer(3, TimeUnit.SECONDS)) |
| 58 | + .subscribe(next -> System.out.printf("next: %s\n", next), // onNext |
| 59 | + throwable -> System.out.printf("error: %s", throwable), //onError |
| 60 | + () -> System.out.println("Completed") //onComplete |
| 61 | + ); |
| 62 | +``` |
| 63 | +## skipWhile |
| 64 | + |
| 65 | +**Available in:**  `Flowable`,  `Observable`,  `Maybe`,  `Single`,  `Completable` |
| 66 | + |
| 67 | +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/skipwhile.html](http://reactivex.io/documentation/operators/skipwhile.html) |
| 68 | + |
| 69 | +discard items emitted by an Observable until a specified condition is false, then emit the remainder |
| 70 | + |
| 71 | +```java |
| 72 | +Observable.range(1, 10).skipWhile(next -> next < 5) |
| 73 | + .subscribe(next -> System.out.printf("next: %s\n", next), // onNext |
| 74 | + throwable -> System.out.printf("error: %s", throwable), //onError |
| 75 | + () -> System.out.println("Completed") //onComplete |
| 76 | + ); |
| 77 | +``` |
| 78 | + |
| 79 | +## takeUntil |
| 80 | + |
| 81 | +**Available in:**  `Flowable`,  `Observable`,  `Maybe`,  `Single`,  `Completable` |
| 82 | + |
| 83 | +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/takeuntil.html](http://reactivex.io/documentation/operators/takeuntil.html) |
| 84 | + |
| 85 | +emits the items from the source Observable until a second Observable emits an item or issues a notification |
| 86 | + |
| 87 | +```java |
| 88 | +Observable.range(1, 10).takeUntil(value -> value >= 5) |
| 89 | + .subscribe(next -> System.out.printf("next: %s\n", next), // onNext |
| 90 | + throwable -> System.out.printf("error: %s", throwable), //onError |
| 91 | + () -> System.out.println("Completed") //onComplete |
| 92 | + ); |
| 93 | +``` |
| 94 | + |
| 95 | +## takeWhile |
| 96 | + |
| 97 | +**Available in:**  `Flowable`,  `Observable`,  `Maybe`,  `Single`,  `Completable` |
| 98 | + |
| 99 | +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/takewhile.html](http://reactivex.io/documentation/operators/takewhile.html) |
| 100 | + |
| 101 | +emit items emitted by an Observable as long as a specified condition is true, then skip the remainder |
| 102 | + |
| 103 | +```java |
| 104 | + Observable.range(1, 10).takeWhile(value -> value <= 5) |
| 105 | + .subscribe(next -> System.out.printf("next: %s\n", next), // onNext |
| 106 | + throwable -> System.out.printf("error: %s", throwable), //onError |
| 107 | + () -> System.out.println("Completed") //onComplete |
| 108 | + ); |
| 109 | +``` |
16 | 110 |
|
17 | 111 | ### Boolean Operators
|
18 |
| -* [**`all( )`**](http://reactivex.io/documentation/operators/all.html) — determine whether all items emitted by an Observable meet some criteria |
19 |
| -* [**`contains( )`**](http://reactivex.io/documentation/operators/contains.html) — determine whether an Observable emits a particular item or not |
20 |
| -* [**`exists( )` and `isEmpty( )`**](http://reactivex.io/documentation/operators/contains.html) — determine whether an Observable emits any items or not |
21 |
| -* [**`sequenceEqual( )`**](http://reactivex.io/documentation/operators/sequenceequal.html) — test the equality of the sequences emitted by two Observables |
| 112 | + |
| 113 | +### Outline |
| 114 | + |
| 115 | +- [`all`](#all) |
| 116 | +- [`contains`](#contains) |
| 117 | +- [`isEmpty`](#isEmpty) |
| 118 | +- [`sequenceEqual`](#sequenceEqual) |
| 119 | + |
| 120 | +## all |
| 121 | +**Available in:**  `Flowable`,  `Observable`,  `Maybe`,  `Single`,  `Completable` |
| 122 | + |
| 123 | +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/all.html](http://reactivex.io/documentation/operators/all.html) |
| 124 | + |
| 125 | +determine whether all items emitted by an Observable meet some criteria |
| 126 | + |
| 127 | +```java |
| 128 | +Flowable.range(0,10).doOnNext(next -> System.out.println(next)).all(integer -> integer<10). |
| 129 | + blockingSubscribe(success->System.out.println("Success: "+success)); |
| 130 | +``` |
| 131 | + |
| 132 | +## contains |
| 133 | +**Available in:**  `Flowable`,  `Observable`,  `Maybe`,  `Single`,  `Completable` |
| 134 | + |
| 135 | +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/contains.html](http://reactivex.io/documentation/operators/contains.html) |
| 136 | + |
| 137 | +determine whether an Observable emits a particular item or not |
| 138 | + |
| 139 | +```java |
| 140 | +Flowable.range(1,10).doOnNext(next->System.out.println(next)) |
| 141 | + .contains(4).blockingSubscribe(contains->System.out.println("contains: "+contains)); |
| 142 | +``` |
| 143 | + |
| 144 | +## isEmpty |
| 145 | +**Available in:**  `Flowable`,  `Observable`,  `Maybe`,  `Single`,  `Completable` |
| 146 | + |
| 147 | +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/contains.html](http://reactivex.io/documentation/operators/contains.html) |
| 148 | + |
| 149 | +determine whether the source Publisher is empty |
| 150 | + |
| 151 | +```java |
| 152 | +Flowable.empty().isEmpty().subscribe(isEmpty -> System.out.printf("isEmpty: %s", isEmpty)); |
| 153 | +``` |
| 154 | + |
| 155 | +## sequenceEqual |
| 156 | +**Available in:**  `Flowable`,  `Observable`,  `Maybe`,  `Single`,  `Completable` |
| 157 | + |
| 158 | +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/sequenceequal.html](http://reactivex.io/documentation/operators/sequenceequal.html) |
| 159 | + |
| 160 | +test the equality of the sequences emitted by two Observables |
| 161 | + |
| 162 | +```java |
| 163 | +Flowable<Integer> flowable1 = Flowable.range(1,3).doOnNext(next-> System.out.print("flowable1: "+next + " ")); |
| 164 | + |
| 165 | +Flowable<Integer> flowable2 = Flowable.range(1,3).doOnNext(next-> System.out.println("flowable2: "+next)); |
| 166 | + |
| 167 | +Flowable.sequenceEqual(Flowable.fromPublisher(flowable1),Flowable.fromPublisher(flowable2)) |
| 168 | + .blockingSubscribe(sequenceEqual->System.out.println("sequenceEqual: "+sequenceEqual)); |
| 169 | +``` |
0 commit comments