10. Appendices
Here we explain how to install the tools used in this document on Windows 7 through 10 machines. Readers should adapt these instructions to their own environment.
10.1. Installing a JDK
The latest JDK can be found at the URL [http://www.oracle.com/technetwork/java/javase/downloads/index.html] (April 2016). We will refer to the JDK installation folder as <jdk-install> hereafter.
![]() |
10.2. Installing the Android SDK Manager
![]() |
- See [1] for why we need the Android SDK;
The Android SDK Manager can be found at [https://developer.android.com/studio/index.html#downloads] (May 2016).
![]() |
Install the SDK Manager. We will refer to its installation directory as <sdk-manager-install> ( ). Launch it.
The project has been configured for (see section 9.3.2):
- SDK API 23 [2];
- SDK Build-tools 23.0.3 [3];
- the SDK Tool 25.1.3 [4]
Make sure you have downloaded these components.
10.3. Installing the Genymotion Emulator Manager
The emulators provided with the Android SDK are slow, which discourages their use. The company [Genymotion] offers a high-performance emulator. It is available at the URL [https://cloud.genymotion.com/page/launchpad/download/] (May 2016).
You will need to register to obtain a version for personal use. Download the [Genymotion] product with the VirtualBox virtual machine:

We will refer to the [Genymotion] installation folder as <genymotion-install> from now on. Launch [Genymotion]. Then download an image for a tablet:
![]() |
- in [1], add the virtual terminal described in [2];
10.4. Installing the IntelliJ IDEA Community Edition IDE
The [IntelliJ IDEA Community Edition] IDE is available at [https://www.jetbrains.com/idea/#chooseYourEdition]:
![]() |
Install the IDE and then launch it.
![]() | ![]() |
- In [1-2], configure the plugins;
- In [3-4], add the [Genymotion] plugin to the IDE;
![]() |
- In [6-7], configure the IDE;
![]() | ![]() |
- In [8-9], specify the installation folder for the [Genymotion] emulator manager;
![]() |
- In [12-13], configure the default project type;
![]() | ![]() |
![]() |
- in [14-16], configure the JDK;
![]() |
![]() | ![]() |
- In [17-20], configure the Android SDK;
![]() |
- In [21-22], specify the default JDK for projects;
![]() | ![]() |
![]() |
- In [23-27], disable spell checking, which is set to English by default;
![]() | ![]() |
- In [28-32], choose the type of keyboard shortcuts you want. You can keep IntelliJ’s default settings or choose those from another IDE you’re more accustomed to;
![]() |
- In [33-35], configure the IDE for multiple projects. It can manage several projects in the same window or in different windows;
![]() |
- In [36-37], enable line numbering by default. This will allow you to quickly find the line that caused an exception;
10.5. Using the Examples
The IntelliJ IDEA projects for the examples are available |HERE|. Section 1.3 explains how to open them.
10.6. Managing JSON in Java
Transparently for the developer, the [Spring MVC] framework uses the [Jackson] JSON library. To illustrate what JSON (JavaScript Object Notation) is, we present here a program that serializes objects into JSON and does the reverse by deserializing the generated JSON strings to recreate the original objects.
The 'Jackson' library allows you to construct:
- the JSON string of an object: new ObjectMapper().writeValueAsString(object);
- an object from a JSON string: new ObjectMapper().readValue(jsonString, Object.class).
Both methods may throw an IOException. Here is an example.
![]() |
The project above is a Maven project with the following [pom.xml] file;
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>istia.st</groupId>
<artifactId>json</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
</project>
- lines 12–16: the dependency that includes the 'Jackson' library;
The [Person] class is as follows:
package istia.st.json;
public class Person {
// data
private String lastName;
private String firstName;
private int age;
// constructors
public Person() {
}
public Person(String lastName, String firstName, int age) {
this.lastName = lastName;
this.firstName = firstName;
this.age = age;
}
// signature
public String toString() {
return String.format("Person[%s, %s, %d]", lastName, firstName, age);
}
// getters and setters
...
}
The [Main] class is as follows:
package istia.st.json;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Main {
// the serialization/deserialization tool
static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws IOException {
// creating a person
Person paul = new Person("Denis", "Paul", 40);
// Display JSON
String json = mapper.writeValueAsString(paul);
System.out.println("JSON=" + json);
// Instantiate Person from JSON
Person p = mapper.readValue(json, Person.class);
// Display Person
System.out.println("Person=" + p);
// an array
Person virginie = new Person("Radot", "Virginie", 20);
Person[] people = new Person[]{paul, virginie};
// display JSON
json = mapper.writeValueAsString(people);
System.out.println("JSON people=" + json);
// dictionary
Map<String, Person> people = new HashMap<String, Person>();
hpeople.put("1", paul);
hpeople.put("2", virginie);
// Display JSON
json = mapper.writeValueAsString(hpeople);
System.out.println("JSON hpeople=" + json);
}
}
Executing this class produces the following screen output:
Key takeaways from the example:
- the [ObjectMapper] object required for JSON/Object transformations: line 11;
- the [Person] --> JSON transformation: line 17;
- the JSON --> [Person] transformation: line 20;
- the [IOException] thrown by both methods: line 13.
Table of Contents
1Introduction 5
1.1Background 5
1.2 Tools Used 6
1.3 The example codes 6
2An example of an introduction 9
2.1 The architecture of the sample application 9
2.2The executable 9
2.3 The synchronous interface 11
2.4 The synchronous call 12
2.5 Testing Synchronous Calls 13
2.6 The Asynchronous Interface and Its Implementation 14
2.7 The Asynchronous Call 16
2.8 Testing Asynchronous Calls 19
2.8.1 with the scheduler [Schedulers.io] 20
2.8.2 with the scheduler [Schedulers.computation] 20
2.8.3 with the scheduler [Schedulers.newThread] 21
2.8.4 with the schedulers [Schedulers.trampoline, Schedulers.immediate] 22
2.9 Edge Cases 22
2.10Conclusion 24
3Signatures of generic classes and methods 27
4Java 8 Lambda Expressions 32
4.1Example-01 - Functional interfaces and lambdas 32
4.2Example-02 - The Predicate<T> functional interface 34
4.3 Example-03 - The Functional Interface Function<T,R> 37
4.4Example-04 - The Consumer<T> functional interface 38
4.5 Example-05 - The functional interface BiConsumer<T,U> 40
4.6Example-06 - The BiFunction<T,U,R> functional interface 41
4.7Example-07 - The Supplier<T> functional interface 43
5The Stream<T> type in Java 8 45
5.1Example-01 - The Stream Class 45
5.2Example-02 - Parallel Processing of Stream Elements 47
5.3 Example 3 - Parallel Processing of Stream Elements 48
5.4Example-04 - Filtering a Stream 50
5.5Example-05 - Creating a Stream<T2> from a Stream<T1> 52
5.6Example-06 - Other methods of the Stream<T> class 53
5.6.1 [findFirst] 54
5.6.2 [findAny] 55
5.6.3[skip] 56
5.6.4[limit] 58
5.6.5[count] 59
5.6.6[max, min] 60
5.6.7[reduce] 63
5.6.8[sorted] 63
5.6.9[anyMatch, noneMatch, allMatch] 65
5.6.10[collect(Collectors.groupingBy)] 65
5.6.11[distinct] 67
5.6.12[flatMap] 68
5.6.13 Primitive Number Stream Methods 71
6Functional Interfaces in the RxJava Library 72
6.1 Example-01: The [Action0] Functional Interface 72
6.2Examples 02 and 03: The [Actioni] functional interface 73
6.3 Example-04, 05: The functional interface [Funci] 74
7The RxJava library 77
7.1 Creating and Subscribing to Observables 77
7.1.1Example-01: The [Observable.from] Method 77
7.1.2Example-03: The Observer class 82
7.1.3Example-04: the [Observable.create] method 84
7.1.4Example-05: Refactoring of [Example-04] 86
7.2 Execution Thread, Observation Thread 88
7.2.1Example-06: Observable and Observer in a Thread Other Than [main] 88
7.2.2Example-07: Observable and observer in two different threads 90
7.3 Predefined Observables 92
7.3.1Example-08: The [Observable.range] method 92
7.3.2Example-09: The Observable.[interval, take, doNext] methods 96
7.3.3Examples-10/12: the Observable.[error, empty, never] methods 98
7.4 Multithreading 102
7.4.1Example-13: Action thread, observation thread 103
7.5 Combinations of Multiple Observables 106
7.5.1Example 14: Merging two observables with [Observable.merge] 106
7.5.2Example 15: Concatenating two observables with [Observable.concat] 108
7.5.3 Example 16: Combining two observables using [Observable.zip] 109
7.5.4 Example 17: Combining two observables with [Observable.combineLatest] 111
7.5.5Example-18: Combining two observables with [Observable.amb] 113
7.6 Processing chain for an observable 114
7.6.1Example 19: Transforming an observable with [Observable.map] 114
7.6.2Example 20: Filtering an observable with [Observable.filter] 116
7.6.3Example-21: Transforming an observable with [Observable.flapMap] 117
7.6.4Example 22: Other methods of the [Observable] class 123
7.7 Schedulers 127
7.7.1Example-23: The [Schedulers.computation] scheduler 127
7.7.2 Example 24: The [Schedulers.io] scheduler 128
7.7.3 Example 25: The scheduler [Schedulers.newThread] 129
7.7.4Example 26: Schedulers [Schedulers.immediate, Schedulers.trampoline] 130
7.8Conclusion 133
8RxJava in the Swing Environment 134
8.1 Introduction 134
8.2 Code Structure 135
8.3 Project Execution 136
8.4 The Synchronous Service 136
8.5 The Asynchronous Service 139
8.6 The graphical user interface 141
8.7 Instantiating the graphical user interface 143
8.8Executing Synchronous Requests 144
8.9 Executing Asynchronous Requests 145
9RxJava in the Android Environment 149
9.1 Introduction 149
9.2 The Web Service / JSON 149
9.2.1The IntelliJ IDEA Project 150
9.2.2 The Project's Gradle Dependencies 151
9.2.3The [business] layer 153
9.2.4 The Web Service / JSON 156
9.2.5 Spring Project Configuration 160
9.2.6Running the Web Server 161
9.3 The Android Client 161
9.3.1 RxAndroid 161
9.3.2 The IntelliJ IDEA Project 162
9.3.3Running the IntelliJ IDEA Project 164
9.3.4The project's Gradle dependencies 166
9.3.5 The Android Application Manifest 167
9.3.6The [DAO] layer 168
9.3.6.1The [IDao] Interface of the [DAO] Layer 168
9.3.6.2Implementation of the [DAO] layer 170
9.3.7Application Views 172
9.3.7.1The [MyFragment] Class 174
9.3.7.2The [RequestFragment] fragment of the request 176
9.3.7.3The [ResponseFragment] of the response 177
9.3.7.4The Android [MainActivity] 178
9.3.7.5 The [RequestFragment] fragment 185
9.3.7.6 The Fragment [ResponseFragment] 187
9.3.8 Examples of Observables 190
9.3.8.1Example-01 190
9.3.8.2Example-02 193
9.3.8.3Example-03 195
9.3.8.4Example-04 197
9.3.8.5Example-05 198
9.3.8.6To continue 202
9.3.9Conclusion 202
10Appendices 203
10.1 Installing a JDK 203
10.2 Installing the Android SDK Manager 203
10.3 Installing the Genymotion Emulator Manager 204
10.4 Installing the IntelliJ IDEA Community Edition IDE 205
10.5Using the Examples 210
10.6 Working with JSON in Java 211

























