Archive Monthly Archives: October 2018

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 […]

RxJava Operators: count(), reduce()

October 24, 2018

count() The operator count() is one of the operators which take a series of emissions and consolidate them into a single emission, usually emitted via a Single. As the name suggests, this operator counts the number of emissions: import io.reactivex.Observable; public class Main { public static void main(String[] args) { Observable<String> observable = Observable.just(“a”, “b”, “c”, […]

RxJava Operators: delay(), repeat(), repeatWhen()

October 23, 2018

delay() With the help of the delay() operator emissions can be shifted forward in time. import io.reactivex.Observable; import java.util.concurrent.TimeUnit; public class Main { public static void main(String[] args) throws InterruptedException { Observable<String> myObservable = Observable.just(“a”, “b”, “c”); myObservable.delay(2, TimeUnit.SECONDS) .subscribe(System.out::println); Thread.sleep(4000); } } It is possible to delay the error notifications as well, you just need […]

RxJava Operators: sorted()

October 22, 2018

sorted() For finite Observables which emit items that implement the Comparable interface, it is possible to sort them with the help of the sorted() operator. It collects all emissions and then emit them in their sorted order: import io.reactivex.Observable; public class Main { public static void main(String[] args) { Observable<Integer> observable = Observable.just(5, 3, 4, 2, 1); […]

RxJava Operators: startWith(), defaultIfEmpty(), switchIfEmpty()

October 21, 2018

startWith() With the help of the startWith() operator it is possible to insert an emission before all other emissions: import io.reactivex.Observable; public class Main { public static void main(String[] args) { Observable<String> myObservable = Observable.just(“a”, “b”, “c”); myObservable.startWith(“First!”) .subscribe(System.out::println); } } In case you need to start with multiple items, you can use the startWithArray() […]

1 2 3
Bitnami