February 2020
Intermediate to advanced
412 pages
9h 36m
English
The reduce() operator is syntactically identical to scan(), but it only emits the final result when the source Observable calls onComplete(). Depending on which overloaded version is used, it can yield Single or Maybe. If you need the reduce() operator to emit the sum of all emitted integer values, for example, you can take each one and add it to the rolling total. But it will only emit once—after the last emitted value is processed (and the onComplete event is emitted):
import io.reactivex.rxjava3.core.Observable;public class Ch3_23 { public static void main(String[] args) { Observable.just(5, 3, 7) .reduce((total, i) -> total + i) .subscribe(s -> System.out.println("Received: " + s)); }}
The output of the preceding code snippet ...
Read now
Unlock full access