博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RxJava flatMap,switchMap,concatMap
阅读量:2534 次
发布时间:2019-05-11

本文共 7360 字,大约阅读时间需要 24 分钟。

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
race = new ArrayList<>(Arrays.asList("Alan", "Bob", "Cobb", "Dan", "Evan", "Finch")); Observable.from(race) .flatMap(new Func1
>() { @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
>) 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
>) 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:

参考:

翻译自:

转载地址:http://hqmzd.baihongyu.com/

你可能感兴趣的文章
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
自动测试用工具
查看>>
前端基础之BOM和DOM
查看>>
[T-ARA/筷子兄弟][Little Apple]
查看>>
编译Libgdiplus遇到的问题
查看>>
【NOIP 模拟赛】Evensgn 剪树枝 树形dp
查看>>
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
两台电脑如何实现共享文件
查看>>
组合模式Composite
查看>>
程序员最想得到的十大证件,你最想得到哪个?
查看>>
我的第一篇CBBLOGS博客
查看>>
【MyBean调试笔记】接口的使用和清理
查看>>
07 js自定义函数
查看>>
jQueru中数据交换格式XML和JSON对比
查看>>
form表单序列化后的数据转json对象
查看>>
[PYTHON]一个简单的单元測试框架
查看>>
iOS开发网络篇—XML数据的解析
查看>>
[BZOJ4303]数列
查看>>