Archive Monthly Archives: November 2018

RxJava Operators: cache()

November 26, 2018

cache() The cache() operator can be used to cache all emissions indefinitely. It will subscribe to the source on the first downstream subscribing¬†Observer. import io.reactivex.Observable; public class Main { public static void main(String[] args) { Observable<String> myCachedSource = Observable.just(“a”, “b”, “c”, “d”, “e”) .scan((total, next) -> { System.out.println(String.format(“Calculating %s + %s”, total, next)); return total […]

RxJava Operators: replay()

November 19, 2018

replay() The replay() operator can be used to keep previous emissions and then emit them again when a new Observer comes in the scope. It returns a ConnectableObservable, so it will multicast the emissions and also emit the previous items. In the example below the first Observer will start thanks to the autoConnect() on the […]

RxJava Operators: refCount(), share()

November 18, 2018

refCount() and share() The refCount() operator behaves like an autoConnect(1) operator, but there is an important difference: when it has no Observers anymore, it will dispose itself and start over when a new Observer subscribes. import io.reactivex.Observable; import java.util.concurrent.TimeUnit; public class Main { public static void main(String[] args) throws InterruptedException { Observable<Long> mySource = Observable.interval(1, […]

RxJava Operators: autoConnect()

November 16, 2018

autoConnect() To make sure that a ConnectableObserverable automatically calls the connect() method after a given number of Observers subscribe, the autoConnect() operator can be used. import io.reactivex.Observable; import java.util.concurrent.ThreadLocalRandom; public class Main { public static void main(String[] args) { Observable<Integer> mySource = Observable.range(1, 3) .map(x -> ThreadLocalRandom.current().nextInt(1, 100)) .publish() .autoConnect(2); mySource.subscribe(x -> System.out.println(String.format(“Observer 1: %d”, […]

RxJava Multicasting

November 15, 2018

Multicasting Multicasting means consolidating the stream of emissions for all Observers to be the same. In the post about hot and cold Observables, we have already seen an example of multicasting: import io.reactivex.observables.ConnectableObservable; public class Main { public static void main(String[] args) throws InterruptedException { ConnectableObservable<String> myObservable = Observable.just(“A”, “B”, “C”, “D”, “E”).publish(); myObservable.subscribe(s -> […]

RxJava Operators: groupBy()

November 14, 2018

groupBy() With the help of the groupBy() operator, it is possible to group emissions by a given key into seperate Observables. After the grouping, the returned type is an Observable<GroupedObservable<K,T>>¬†where K is the key and T is the type of the mapped emissions for that given key. import io.reactivex.Observable; public class Main { public static […]

RxJava Operators: withLatestFrom()

November 13, 2018

withLatestFrom() The operator withLatestFrom() is quite similar to combineLatest(): it maps each emission with the latest value from the other Observables and combines them. The key difference compared to combineLatest() is that it will take only one emission from each Observable. import io.reactivex.Observable; public class Main { public static void main(String[] args) { Observable<String> mySource1 […]

RxJava Operators: combineLatest()

November 12, 2018

combineLatest() The combineLatest() operator is quite similar to the zip() operator, but for every item which is emitted from one of the sources, it will be immediately coupled with the latest emissions from the other sources. This operator will not queue up unpaired emissions for each source, but cache the items and pair only the […]

RxJava Operators: Zipping

November 11, 2018

Zipping() Zipping allows us to take emissions from Observable which emit even different type of items and combine them into a single emission. import io.reactivex.Observable; public class Main { public static void main(String[] args) { Observable<String> mySource1 = Observable.just(“a”, “b”, “c”, “d”, “e”); Observable<Integer> mySource2 = Observable.range(1, 10); Observable.zip(mySource1, mySource2, (s1, s2) -> s1 + […]

RxJava Operators: Ambiguous

November 9, 2018

amb() The amb() operator takes an Iterable<Observable> as parameter and will emit the emissions of the first Observable which emits anything and disposes the other Observables. A possible use-case for this operator could be that you have multiple sources for the same items and you don’t care which source provides the answer, you just want […]

Bitnami