RxJava Operators: concat(), concatWith()

November 8, 2018

concat() The concat() operator is quite similar to merging, but it will not move on to the next Observable until the current one calls onComplete(). This means that the concatenated Observables‘ emissions will be in a guaranteed order. If we would use the concat() operator on infinite Observables, it would keep emitting from the first infinite […]

RxJava Operators: flatMap()

November 7, 2018

flatMap() The flatMap() operator is somewhat similar to the map() operator: the map() operator transforms the emitted items into other items, the flatMap() operators transforms the items into Observables. It performs this by performing a merge() operation (this is the flattening part) on the item after wrapping it into an Observable, thus creating a single stream. […]

RxJava Operators: Merging

November 6, 2018

Operators for merging It is possible to merge multiple Observables into one single Observable. This new Observable will subscribe to all of it’s sources simultaneously. It is possible to merge both finite and infinite Observables. merge() and mergeWith() The operator merge() takes two or more Observable<T> sources and merges them into a single Observable<T>. import io.reactivex.Observable; public class […]

RxJava Operators: doOnSubscribe, doOnSuccess(), doOnDispose() and doFinally()

November 5, 2018

doOnSubscribe, doOnDispose() and doFinally() With the help of the operators doOnSubscribe() and doOnDispose() it is possible to execute code when a subscription and disposal occur: import io.reactivex.Observable; public class Main { public static void main(String[] args) { Observable<String> myObservable = Observable.just(“a”, “b”, “c”, “d”); myObservable.doOnSubscribe(disposable -> System.out.println(“Subscribed!”)) .doOnDispose(() -> System.out.println(“Disposing!”)) .subscribe(System.out::println); } } In the […]

RxJava Operators: doOnNext(), doAfterNext(), doOnComplete(), doOnError()

October 31, 2018

doOnNext() The doOnNext() operator can be used for side-effects in the operator chain. This operator does not affect or transfer the emission, but let’s us to take a peek at the emission. One possible use is to include logging: import io.reactivex.Observable; public class Main { public static void main(String[] args) { Observable<String> myObservable = Observable.just(“a”, […]

RxJava Operators: Error recovery

October 30, 2018

Operators for error recovery Errors might occure during the processing of the Observables. When an error occurs the onError() is communicated down the Observable chain to the Observer and then the subscription terminates and no more emissions will occur. There are cases when we want to handle these exceptions before they get to the Observer, […]

RxJava Operators: collect()

October 29, 2018

collect() In case you need a custom collection operator, you can use collect(). Two arguments are needed: an initialValueSupplier which creates the empty collection you want to use and a collector which specifies how each emitted item should be added to the collection. For example if you want to collect elements into a HashSet: import […]

RxJava Operators: toMap(), toMultiMap()

October 28, 2018

toMap() The toMap() operator collects the emissions into a Map<K, T>, where the K key is derived from the supplied Lambda function: import io.reactivex.Observable; public class Main { public static void main(String[] args) { Observable<String> myObservable = Observable.just(“aaa”, “b”, “cc”, “dddd”); myObservable.toMap(x -> x.charAt(0)) .subscribe(System.out::println); } } It is possible to associate an another value […]

RxJava Operators: toList(), toSortedList()

October 26, 2018

toList() Certain operators can be used to collect all emissions into a collection. Such operator is the toList() operator. By default the toList() operator will use the ArrayList implementation. import io.reactivex.Observable; public class Main { public static void main(String[] args) { Observable<String> myObservable = Observable.just(“a”, “b”, “c”); myObservable.toList() .subscribe(System.out::println); } } It is possible to […]

RxJava Operators: contains(), any(), all()

October 25, 2018

contains() The contains() operator checks if a specific element is emitted by an Observable: import io.reactivex.Observable; public class Main { public static void main(String[] args) { Observable<String> myObservable = Observable.just(“a”, “b”, “c”, “d”, “e”); myObservable.contains(“a”) .subscribe(System.out::println); } } any() The any() operator will check if at least one emitted item meets the specified condition. When […]

Bitnami