Understanding Java: RxJava for beginners

In this article I will explain basics of RxJava though examples. This article is for RxJava beginners. On the other hand, if you already know something about RxJava, this article is good for reviewing your knowledge.

What is RxJava?

RxJava is a Java VM implementation of ReactiveX a library for composing asynchronous and event-based programs by using observable sequences.

Example 1

In this example we will create basic Observable and Subscriber, and then we will link them up, and see what will happen. Spoiler alert! Something awesome…

Can I do it more simple?

Of course! You can do it!

Example 2

In this example we will make the same thing like in Example 1, but now with less code.

Example 3

In this example we will do something little bit differently with Observable.from(). The idea is to print strings „Blue“, „Factory“, „Blog“, „Post“.
Let’s remind ourselves what is Observable.from(). Observable.from() receives items list and emits on item by one, so Observable.from() is exactly what we need!
First we will create ArrayList with Strings:

What are Operators in Rx?

Operators are methods created for solving transformations and handling API calls problems. I will show you one simple example for transformation with Map, and maybe in some further articles examples of handling API calls with Retrofit, RxJava on MVP architectural pattern.

Example 4

First let’s create Observable:

Example 5

Example 6

In this example Observable will emit String “5” and Operator will transform it to the Integer 5. Let’s crate Observable.

What can I do with RXJava?

You can do everything you want because your Observable can be everything, it can be: String, Integer, API call, click, etc. As you progress like Android developer you will use RxJava mostly for API calls and for android widgets. RxJava provides easy API handling, because you don’t need to worry about threading and you can chain few requests and get result on single Subscriber. With RxJava you can handle some widget behavior for example what widget will do after five times being pressed, or you can create Observable which will emits String every 4 sec from EditText input, and so many other fancy things. I hope this article gives you basics of RxJava and I hope you will keep exploring RxJava in the future.

For more please check: http://plavatvornica.com/rxjava-for-beginners/

We design and develop native mobile & web applications for startups and successful companies.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store