February 2020
Intermediate to advanced
412 pages
9h 36m
English
The scan() method is a rolling aggregator. It adds every emitted item to the provided accumulator and emits each incremental accumulated value. For instance, let's emit the rolling sum of all of the values emitted so far, including the current one, as follows:
import io.reactivex.rxjava3.core.Observable;public class Ch3_20 { public static void main(String[] args) { Observable.just(5, 3, 7) .scan((accumulator, i) -> accumulator + i) .subscribe(s -> System.out.println("Received: " + s)); }}
The output of the preceding code snippet is as follows:
Received: 5Received: 8Received: 15
As you can see, first, the scan() operator emitted the value of 5, which was the first value it received. Then, it received 3 and added it to 5, emitting
Read now
Unlock full access