We will see the following operators on Observables:

  • map (and flatMap)
  • cast
  • startWith
  • defaultIfEmpty
  • switchIfEmpty
  • sorted
  • delay
  • repeat
  • scan
  • count
  • reduce
  • all
  • any
  • contains

map (and flatMap)

map: Convert the Observable into an Observable using a lambda Function<T,R>. One item of T type will be converted into one item of R type. It’s one to one relation.

flatMap: Convert every item of the Observable into an Observable. So the lambda Function is <T,Observable> and you ends up with a merge of Observable. It’s a one to many relation. We will see this notion later.

So using map you can convert a specific stream of object T into another stream of object R.

cast

It helps to cast the Observable to Observable.
Mainly it’s usefull when you have a mother class (or interface) and cast all the elements into this class.

startWith

It helps to start the emission with a specific item.

defaultIfEmpty and switchIfEmpty

It helps to emits an element if the original stream is empty.
defaultIfEmpty: Helps you emits a specific item if the stream is empty; onComplete is called even if no item has been emitted.
switchIfEmpty: Helps you switch to an other observable if the stream is empty; onComplete is called even if no item has been emitted.

defaultIfEmpty

Simple example on how to use defaultIfEmpty.Has to wait the onComplete.

switchIfEmpty

Simple example on how to use switchIfEmpty by returning a new streams of items. Has to wait the onComplete.

sorted

It helps you sorts the items emitted. Has to wait the onComplete. Sorted objects need to implement comparable or you can provide it.

delay

Delay: It helps you delay the emission of the items.
By default the delay applies only on the emission of the items, the errors are immediately fired. but you can also specify if error needs to have a delay.

repeat

Repeat: It helps you delay the emission of the items.
By default the delay applies only on the emission of the items, the errors are immediately fired. but you can also specify if error needs to hae a delay.

scan

Scan: It’s a reducing operator.
It’s based on a lambda function which take the item emitted and concatenate it with an accumulator:(accumulator,next)->accumulator + next.
The accumulator is emitted for every item received.
The accumulator is initialized with the first item.
You can provide an initial value, it will be emitted first and used for the accumulation. This is the trick to change the item emitted type.

reduce

Reduce: It helps you accumulate the item into an accumulator which will be emitted when onComplete is reached on the initial stream.
reduce==scan().takeLast(1)
Reduce returns a Single or MayBE.
It’s based on a lambda function which take the item emitted and concatenate it with an accumulator:(accumulator,next)->accumulator + next.
The accumulator is emitted when the last item is received, it needs to wait the onComplete.
The accumulator is initialized with the first item, if no initial value is provided.
You can provide an initial value, it will be used for the accumulation. This is the trick to change the item emitted type.

count

count: It returns the number of emitted elements. It has to wait the onComplete to fire it.
count returns a Single.

all,any,contains

all: check a condition for every item, if each condition is true, the return is true else it will be false. The emission is done when a condition is calculated as false or when the onComplete is reached(and return true).
any: check a condition for every item, if one condition is true, the return is true else it will be false.The emission is done when a condition is calculated as true or when the onComplete is reached(and return false).
contains: check if the emitted item is the one expected, when the expected item is found, it immediately returns true else it wait onComplete (and return false)
all is a Single
any is a Single
contains is a Single

More on the topic

Previous chapter: Chapter 6: Observable operators

Next chapter: Chapter 8: Observable’s collection transformers operators

This chapter is a small part of a biggest training project, you can find on Github. You’ll have the main branch with the answers/responses/unit tests and documentation. You could start with the “questions” branch to only have the questions.

You’ll have the full table of content of the articles on Medium.

You can download the book on Android2ee

Warning: Those articles has been created only because I am stuck in a Rx Android project and I need to prepare the ramp up for those who will replace me.

My conclusion : Choose Kotlin and suspending function instead of Rx. PLEASE, save your project from RxHell !!!

Write Medium in Markdown? Try Markdium!

--

--

android2ee (Seguy Mathias)

Android2ee alias Mathias Seguy. Android expert gives you some news on the Android world. mathias.seguy@android2ee.com