Book Image

Learning RxJava

By : Thomas Nield
Book Image

Learning RxJava

By: Thomas Nield

Overview of this book

RxJava is a library for composing asynchronous and event-based programs using Observable sequences for the JVM, allowing developers to build robust applications in less time. Learning RxJava addresses all the fundamentals of reactive programming to help readers write reactive code, as well as teach them an effective approach to designing and implementing reactive libraries and applications. Starting with a brief introduction to reactive programming concepts, there is an overview of Observables and Observers, the core components of RxJava, and how to combine different streams of data and events together. You will also learn simpler ways to achieve concurrency and remain highly performant, with no need for synchronization. Later on, we will leverage backpressure and other strategies to cope with rapidly-producing sources to prevent bottlenecks in your application. After covering custom operators, testing, and debugging, the book dives into hands-on examples using RxJava on Android as well as Kotlin.
Table of Contents (21 chapters)
Title Page
Credits
About the Author
Acknowledgements
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

Transforming operators


Next, we will cover various common operators that transform emissions. A series of operators in an Observable chain is a stream of transformations. You have already seen map(), which is the most obvious operator in this category. We will start with that one.

map()

For a given Observable<T>, the map() operator will transform a T emission into an R emission using the provided Function<T,R> lambda. We have already used this operator many times, turning strings into lengths. Here is a new example: we can take raw date strings and use the map() operator to turn each one into a LocalDate emission, as shown in the following code snippet:

import io.reactivex.Observable;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Launcher {
      public static void main(String[] args) {

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/d
          /yyyy");

        Observable.just("1/3/2016", "5/9/2016", "10/12/2016")
       ...