Archive Monthly Archives: October 2018

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

Speacial Observables: Single, Maybe and Completable

October 12, 2018

There are a few special Observables available in RxJava: Single, Maybe and Completable. Single Single is an Observable that will emit only one item: it consolidates onNext() and onComplete() into a single event, so on subscribing to a Single, we can provide a Lambda for the onSuccess() and to the optional onError(): import io.reactivex.Single; public […]

Observable factories

October 11, 2018

There are various factory methods available to create Observables. In this post we will go through some of them. Observable.range() and rangeLong() In a previous post, we created an Observable using an IntStream. To emit a consecutive range of integers, there is a shorter solution available: import io.reactivex.Observable; public class Main { public static void […]

Cold and Hot Observables

October 10, 2018

A major characteristic of Observables is how they behave when there are multiple Observers. Based on this, we differentiate between Hot and Cold Observables. Cold Observables You can imagine Cold Observables like a DVD disk with a movie on it. Each viewer of the DVD can watch the whole movie at any time. Similarly, Cold […]

Bitnami