DEV Community

Cover image for map vs flatMap vs concatMap in RxJava With Kotlin
Sherif Shahin
Sherif Shahin

Posted on

map vs flatMap vs concatMap in RxJava With Kotlin

map

  • work like kotlin's standard map function but with observables instead of collection.
  • transform the items by applying a function to each item.

example in Kotlin

        // create subscriber to Observable
        val subscriber = Observable.just(1,2,3,4,5)
                // use map to transform int values to random values
                .map {
                    it * 10
                }
                .subscribeBy(
                        onNext = {println(it.toInt())}
                )
Enter fullscreen mode Exit fullscreen mode

the output will be :

10
20
30
40
50
Enter fullscreen mode Exit fullscreen mode

but what if I want to return observables and subscribe all of them and merge the result?!

flatMap

Transform the items emitted by an observable into observables and keeps observing on each observable then merges the resulting observables sequences into one observable sequence.

example in Kotlin

 // create athletic class which contains subject to store athletic's sports
        class Athletic(val sports : PublishSubject<String>)

        // create subject object take Athletic datatype
        val subject = PublishSubject.create<Athletic>()


        val subscriber = subject.
                // use flatMap to transform athletic object to sports observable and subscribe it  
                flatMap {
                    it.sports
                }.subscribe {
                    println(it)
                }

        // create two Athletic objects
        val sherif = Athletic(PublishSubject.create())
        val john = Athletic(PublishSubject.create())

        // pass objects to the subject
        subject.onNext(sherif)
        subject.onNext(john)

        sherif.sports.onNext("football")
        john.sports.onNext("volleyball")
        sherif.sports.onNext("basketball")
Enter fullscreen mode Exit fullscreen mode

the output will be:

football
volleyball
basketball
Enter fullscreen mode Exit fullscreen mode

if we looked at the result we will find that flatmap keeps up with each observable it creates.

but what if I Want to subscribe them but get data in sequential order?!

concatMap

it's closely similar to flatMap with a little bit difference which Transform the items emitted by an observable into observables and keeps observing on each observable then merges the resulting observables sequences into one observable sequence but each sequence produced by the lambda function of concatMap will run to completion before move to the next sequence.

example in Kotlin

    // create observable emit number's types
        val numbers = Observable.just("odd" ,"even")

        // create subscriber to numbers
        val subscriber = numbers.
                concatMap { type ->
                    when(type){
                        "odd" -> {
                            println("**odd**")
                            Observable.just(1,3,5,7,9)
                        }
                        "even" -> {
                            println("**even**")
                            Observable.just(2,4,6,8,10)
                        }
                        else -> Observable.empty()
                    }
                }.subscribe {number ->
                    println(number)
                }
Enter fullscreen mode Exit fullscreen mode

the output will be

**odd**
1
3
5
7
9
**even**
2
4
6
8
10
Enter fullscreen mode Exit fullscreen mode

if we looked at the result we will find that the odd numbers all printed followed by the even number.

and that's it , I hope you've learned something new, please drop me a comment if you didn't understand something, I also love to hear suggestions for improving, so if you have any, please drop me a comment.

Top comments (0)