The map operator

The map operator performs a given task (lambda) on each of the emitted items and emits them to the downstream. We have already seen a little use of the map operator. For a given Observable<T> or Flowable<T>, the map operator will transform an emitted item of type T into an emission of type R by applying the provided lambda of Function<T,R> to it.

So, now, let's take a look at another example with the map operator:

    fun main(args: Array<String>) { 
      val observable = listOf(10,9,8,7,6,5,4,3,2,1).toObservable() 
      observable.map {//(1) 
        number-> "Transforming Int to String $number" 
      }.subscribe { 
        item-> println("Received $item") 
      } 
    }

On comment (1), we used the map operator, which will transform the emitted item of type Int to an emission of type String. Although we have a clear idea of what the output will be, let's validate that by taking a look at the following screenshot:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.21.158