O'Reilly logo

Reactive Android Programming by Tadas Subonis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Transforming values with Map

We have used .map() before as well. This is a functional method, and it is really easy to understand what it does--it just transforms one item to another.

Consider the given example:

Observable.just(1, 2, 3)        .map(i -> i + 1)

It will go through each number and return a new value that is higher by one. It is highly encouraged to never modify values in place, in the .map() method, but to always return a new one.

So, this approach will be bad:

Observable.just(new Date(1), new Date(2), new Date())        .map(i -> {            i.setTime(i.getTime() + 1);            return i;        })

Instead of that, prefer the following way:

Observable.just(new Date(1), new Date(2), new Date())        .map(i -> new Date(i.getTime() + 1))

This approach helps to avoid ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required