Gradle Hello World project with RxJava

RxJava is the Java implementation of Rx Framework which was developed at Microsoft. It's a library that allows you build reactive systems.

Some words about Monad

It's no secret that Observables are Monads, Monad is all about abstraction and composition, more accurately, the functor part of a Monad act as container which do the abstraction, there are some extra part, i.e. the flatmap makes it composable. The Observalbe act as a container abstract values in stream from various sources, this is so called monadic stream. All monadic code share similar structure, there is a container with context and structure that wraps values, in this case the stream of values, the container abstract away the awkward stuff revolving around the value, the other side, there are various lifting operator accept pure functions which only interest and depend on the values. There are still debate about whether the Observable is genuinely Monad, just as the Javascript promise object, the takeaway is the core monad principle is the same.

Step 1. Create Java QuickStart project

Create a Gradle project, using the QuickStart template

Step 2. Add RxJava dependencies

Open your build.gradle file and add a line in dependencies block

 
dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
    compile 'io.reactivex:rxjava:1.0.14'
}
 
 

And don't forget to set the source compatiblity to higher than 1.5. Because you will need to implements and override the call method of Func1 class. If you are using Java 8, this can be written in Lambda expression, but if you are using Java 7 and lower version, you need to use anonymous class.

 
sourceCompatibility = 1.7
 

Step 3. Add test class

First create a new package, for example com.makble.rxexamples

Add a class for example RxTest

 
package com.makble.rxexamples;
 
import java.util.Arrays;
 
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;
 
public class RxTest {
    public static void main (String [] args){
 
        Integer[] numbers = {1,2 ,3,4};
        Observable<Integer> source = Observable.from(numbers);
 
        source.map(new Func1<Integer, Integer>(){
            @Override
            public Integer call(Integer i){
                return i + 3;
            }
        })
        .subscribe(new Action1<Integer>(){
            public void call(Integer i){
                System.out.println(i);
            }
        });
    }
}
 
 

We are creating observable object and apply the map operator on it then in subscription we print the value of itmes in the observable sequence.

Using Java 8 lambda syntax and Gradle 5

The observable pattern is a functional programming pattern, the first class function is indispensable language feature for any functional pattern. With Java 8's lambda support we can get rid of the awkward Action1, Func1 code.

Add this line to your dependency block of Gradle 5 build script.

 
    implementation "io.reactivex.rxjava3:rxjava:3.+"
 

The code below create Observable instance from a list:

 
    public static void sayHello(String hello) {
        System.out.println(hello + " world");
    }
    public static void main (String [] args) {
        List<String> ls = new ArrayList<String>();
        ls.add("hello");
        ls.add("bonjour");
 
        Observable<String> obs = Observable.fromIterable(ls);
        obs.subscribe(str -> sayHello(str));
    }