Category Archives for Reactive

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

RxJava Operators: map(), scan()

October 19, 2018

map() The map() operator transforms the emitted T items into R items by using a Function<? super T,? extends R> lambda. For example we could take an Observable which emits Strings and parse these Strings  to LocalDate: import io.reactivex.Observable; import java.time.LocalDate; public class Main { public static void main(String[] args) { Observable<String> dates = Observable.just(“2018-11-12”, “2019-12-12”, […]

RxJava Operators: distinct(), distinctUntilChanged(), elementAt()

October 18, 2018

distinct() The distinct() operator uses the equals() implementation of the emitted objects to emit all unique emissions. import io.reactivex.Observable; public class Main { public static void main(String[] args) { Observable<String> myObservable = Observable.just(“a”, “a”, “b”, “b”, “c”, “c”); myObservable.distinct() .subscribe(System.out::println); } } It is possible to pass a lambda function as parameter to distinct() to […]

RxJava Operators: takeWhile(), skipWhile()

October 17, 2018

In a previous post we have seen the take() and skip() operators. There is another variant of these operators: takeWhile() and skipWhile(). To be precise, actually two other variants exist, because takeUntil() and skipUntil() are also available. takeWhile() This operator will take emissions while a condition based on each emission is true. When it encounters […]

RxJava Operators: filter(), take(), skip()

October 16, 2018

To keep the code as reactive as possible, business logic should be implemented and executing using RxJava operators. These operators are Observers subscribing to the Observable they are called on: they transform the emissions and emit items downstream to other Observers which might be other operators or a final Observer. There are many operators in […]

CompositeDisposable

October 15, 2018

Let’s imagine that you have multiple subscriptions that need to be disposed. In such cases a container would be helpful where you could store all these subscriptions and then dispose all of them at once. Such container is the CompositeDisposable. Internally it holds a collection of Disposables and it guarantees O(1) for addition and removal […]

Disposing

October 14, 2018

After calling the subscribe() method on an Observable to start receiving the emissions, these items pass through the Observable chain. Finite Observables which call the onComplete(), typically free up the resources they were using for garbage collection. But if we were using infinite or long runing Observables, we might be in a situation where we […]

Bitnami