RxJava flatMap,switchMap,concatMap

In this tutorial, we’ll be looking into three important map operators in RxJava. FlatMap, SwitchMap, ConcatMap add more power to the already powerful RxJava framework of operators.

在本教程中,我们将研究RxJava中的三个重要的地图运算符。 FlatMap,SwitchMap,ConcatMap为业已强大的RxJava框架提供了更多功能。

RxJava FlatMap (RxJava FlatMap)

A map operator transforms each of the values from the Observable sequence.
A flatmap operator is used to transform an observable by breaking it into smaller observables containing individual values from the first observable. Then you do stuff on the new observables and finally, the flatmap merges these single observables into one complete observable.
The new observable is now transformed.
FlatMap flattens an observable into single observables which are modified and merged into a new observable.
The new observable that comes out of the flatMap doesn’t guarantee you the same order. The values are interleaved.

map运算符从Observable序列转换每个值。
flatmap运算符用于通过将可观察物分解为较小的可观察物(包含来自第一个可观察物的各个值)来转换它。 然后,对新的可观察对象进行处理,最后, flatmap将这些单个可观察flatmap合并为一个完整的可观察对象。
现在,新的可观察对象已转换。
FlatMap将可观察对象展平为单个可观察对象,然后将其修改并合并为新的可观察对象。
flatMap中出现的新的observable不能保证您具有相同的顺序。 这些值是交错的。

A FlatMap emits Observables instead of values

FlatMap发出Observables而不是值

An illustration for the RxJava documentation is given below.

rxjava flatmap flow

下面给出了RxJava文档的说明。

What exactly does a FlatMap do?
A FlatMap divides an Observable into many singular Observables. Hence, instead of sending each of the Observable values one by one, a FlatMap does everything in parallel and then merges the results in whatever order they are completed.

FlatMap到底做什么?
FlatMap将Observable分为许多奇异的Observable。 因此,FlatMap不会并行发送每个Observable值,而是并行执行所有操作,然后以完成顺序将结果合并。

Let’s look at an example of FlatMap by creating a new IntelliJ java project and adding the RxJava dependency in the build.gradle.

让我们看看FlatMap的例子通过创建一个新的IntelliJ Java项目,并增加在该RxJava依赖build.gradle

import rx.Observable;
import rx.functions.Func1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class RxOperatorMaps {


    public static void main(String[] args) throws InterruptedException {
        final List<String> race = new ArrayList<>(Arrays.asList("Alan", "Bob", "Cobb", "Dan", "Evan", "Finch"));



        Observable.from(race)
                .flatMap(new Func1<String, Observable<?>>() {
                    @Override
                    public Observable<?> call(String s) {
                        final int delay = new Random().nextInt(5);
                        return Observable.just(s).map(String::toUpperCase)
                                .delay(delay, TimeUnit.SECONDS);
                    }
                })
                .subscribe(System.out::println);

        Thread.sleep(5000);
    }
}

//Prints
//BOB
//FINCH
//ALAN
//EVAN
//COBB
//DAN
  • The flatMap applies a function to each observable emitted. The function type is a unit of the source observable type(String in this case) and a new Observable with the type you transform it into.

    flatMap将函数应用于每个可观察到的发射。 函数类型是源可观察类型的单位(在这种情况下为String),并且是将其转换为类型的新Observable的单位。
  • In the above code, a String observable is emitted from the source which was a List of Strings observable.

    在上面的代码中,从源发出了可观察到的字符串,它是可观察到的字符串列表。
  • The new observables are delayed here to show you that a flatMap merges the single observables in the order in which they complete.

    这里将延迟新的可观察对象,以向您显示flatMap将单个可观察对象按其完成的顺序合并。
  • We’ve used a delay operator which delays the emission by a random number of seconds.

    我们使用了一个延迟运算符,它将发射延迟了随机的秒数。
  • We’ve set the main thread to sleep for 5 seconds to prevent the main function from returning while the Observables are still doing their work.

    我们将主线程设置为Hibernate状态5秒钟,以防止在Observable仍在工作时主函数返回。

Note: A flatMap implicitly contains the merge operator too.

注意flatMap隐式包含合并运算符。

Difference between a map and a flatMap operator
Map returns an object of type T whereas a FlatMap returns an Observable<T>.

地图和flatMap运算子之间的差异
Map返回类型T的对象,而FlatMap返回Observable<T>

Using an observable operator we can also filter down the number of values we want to reach the observer.

使用可观察的运算符,我们还可以过滤掉想要到达观察者的值的数量。

Observable.from(race)
                .flatMap(s -> {
                    final int delay = new Random().nextInt(5);

                    if (s.contains("a"))
                        return Observable.empty()
                                .delay(delay, TimeUnit.SECONDS);
                    else
                        return Observable.just(s).map(String::toUpperCase)
                                .delay(delay, TimeUnit.SECONDS);
                })
                .subscribe(System.out::println);

        Thread.sleep(5000);

//Prints
//FINCH
//COBB
//BOB

flatMap would be useful in android when you need to perform multiple network requests in parallel.

当您需要并行执行多个网络请求时,flatMap在android中将非常有用。

RxJava SwitchMap (RxJava SwitchMap)

A SwitchMap flattens the source observable but only returns the last emitted single observable.
The below illustration followed by example demonstrates what the above line means.

SwitchMap将源可观察性展平,但仅返回最后发出的单个可观察性。
下面的插图后面是示例,演示了上面这行的含义。

Observable.from(race)
                .switchMap((Func1<String, Observable<?>>) s -> {
                    final int delay = new Random().nextInt(2);
                    return Observable.just(s).map(String::toUpperCase)
                            .delay(delay, TimeUnit.SECONDS);
                })
                .subscribe(System.out::println);

        Thread.sleep(5000);

//Prints for me. It can differ for you since it's random.
//ALAN
//EVAN
//FINCH

SwitchMap would emit only the latest observable after a particular delay. It ignores all the previous ones.
It is useful when it comes to retrieving the latest data from a feed. It’s useful when you need to identify the latest among many feeds.

在特定的延迟后,SwitchMap将仅发出最新的可观测值。 它忽略所有先前的。
从提要中检索最新数据时,此功能很有用。 当您需要在许多提要中识别最新信息时,它很有用。

RxJava concatMap (RxJava concatMap)

If you’ve read flatMap you almost know what a concatMap operator is. It is similar to a flatMap except that it keeps the order of the observables.
To keep the order, it needs to wait for one observable to finish before proceeding to the next, thereby breaking away from the asynchronous concept and making the task longer than usual.
Another difference between flatMap and concatMap operators
The flatMap uses merge operator implicitly whereas concatMap uses concat operator.

如果您阅读过flatMap,您几乎会知道concatMap运算符是什么。 除了保持可观察对象的顺序外,它与flatMap类似。
为了保持顺序,它需要先等待一个可观察的对象完成,然后再进行下一个观察,从而脱离了异步概念,并使任务比平时更长。
flatMap和concatMap运算符之间的另一个区别
flatMap隐式使用merge运算符,而concatMap使用concat运算符。

Observable.from(race)
                .concatMap((Func1<String, Observable<?>>) s -> {
                    final int delay = new Random().nextInt(4);
                    return Observable.just(s).map(String::toUpperCase)
                            .delay(delay, TimeUnit.SECONDS);
                })
                .subscribe(System.out::println);

        Thread.sleep(5000);

//Prints
//ALAN
//BOB
//COBB

Why did it not print all the values?
Well it took so long that the main thread woke up and exited the main method. You can increase the thread sleep time or decrease the delay random number to view all.
But you get the concept right? ConcatMap takes longer. Use it only if you need the same sequence.

为什么不打印所有值?
好了,花了很长时间才使主线程醒来并退出了main方法。 您可以增加线程睡眠时间或减少延迟随机数以查看全部。
但是您理解正确吗? ConcatMap需要更长的时间。 仅在需要相同序列时才使用它。

Reference: RxJava docs

参考: RxJava文档

翻译自: https://www.journaldev.com/19300/rxjava-flatmap-switchmap-concatmap

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值