June 2017
Intermediate to advanced
400 pages
8h 44m
English
As you might be able to guess, you can cut-off windowed Observables at time intervals just like buffer(). Here, we have an Observable emitting every 300 milliseconds like earlier, and we are slicing it into separate Observables every 1 second. We will then use flatMapSingle() on each Observable to a String concatenation of the emissions:
import io.reactivex.Observable;import java.util.concurrent.TimeUnit;public class Launcher { public static void main(String[] args) { Observable.interval(300, TimeUnit.MILLISECONDS) .map(i -> (i + 1) * 300) // map to elapsed time .window(1, TimeUnit.SECONDS) .flatMapSingle(obs -> obs.reduce("", (total, next) -> total + (total.equals("") ? "" : "|") + next)) .subscribe(System.out::println); ...Read now
Unlock full access