What is the difference between flatmap and switchmap in RxJava?

后端 未结 7 1786
忘掉有多难
忘掉有多难 2020-11-29 15:07

The rxjava doc definition of switchmap is rather vague and it links to the same page as flatmap. What is the difference between the two operators?

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 16:03

    If you´re looking for an example code

    /**
     * We switch from original item to a new observable just using switchMap.
     * It´s a way to replace the Observable instead just the item as map does
     * Emitted:Person{name='Pablo', age=0, sex='no_sex'}
     */
    @Test
    public void testSwitchMap() {
        Observable.just(new Person("Pablo", 34, "male"))
                  .switchMap(person -> Observable.just(new Person("Pablo", 0, "no_sex")))
                  .subscribe(System.out::println);
    
    }
    

    You can see more examples here https://github.com/politrons/reactive

提交回复
热议问题