flatMap(变换操作符)
目录
1 flatMap作用和使用场景
这个方法很有用,例如,当你有一个Publisher,其发射项目本身就是Publisher类型,或者以其他方式可转换为Publisher时,你可以创建一个新的Publisher来完整的发射这些子Publisher项组成的集合
2 flatMap接口
<R> Flowable<R> | flatMap(Function<? super T,? extends Publisher<? extends R>> mapper) Returns a Flowable that emits items based on applying a function that you supply to each item emitted by the source Publisher, where that function returns a Publisher, and then merging those resulting Publishers and emitting the results of this merger. 返回一个Flowable,它根据您为源Publisher发出的每个项目提供的函数应用项目,该函数返回一个Publisher,然后合并生成的Publishers,并发出此合并的结果。 |
<R> Flowable<R> | flatMap(Function<? super T,? extends Publisher<? extends R>> mapper, boolean delayErrors) Returns a Flowable that emits items based on applying a function that you supply to each item emitted by the source Publisher, where that function returns a Publisher, and then merging those resulting Publishers and emitting the results of this merger. 返回一个Flowable,它根据您为源Publisher发出的每个项目提供的函数应用项目,该函数返回一个Publisher,然后合并生成的Publishers,并发出此合并的结果。 |
<R> Flowable<R> | flatMap(Function<? super T,? extends Publisher<? extends R>> mapper, boolean delayErrors, int maxConcurrency) Returns a Flowable that emits items based on applying a function that you supply to each item emitted by the source Publisher, where that function returns a Publisher, and then merging those resulting Publishers and emitting the results of this merger, while limiting the maximum number of concurrent subscriptions to these Publishers. 返回一个Flowable,它根据您提供给源Publisher发出的每个项目的函数来发出项目,该函数返回一个Publisher,然后合并那些生成的Publishers并发出此合并的结果,同时限制最大数量的 对这些发布者的并发订阅。 |
<R> Flowable<R> | flatMap(Function<? super T,? extends Publisher<? extends R>> mapper, boolean delayErrors, int maxConcurrency, int bufferSize) Returns a Flowable that emits items based on applying a function that you supply to each item emitted by the source Publisher, where that function returns a Publisher, and then merging those resulting Publishers and emitting the results of this merger, while limiting the maximum number of concurrent subscriptions to these Publishers. 返回一个Flowable,它根据您提供给源Publisher发出的每个项目的函数来发出项目,该函数返回一个Publisher,然后合并那些生成的Publishers并发出此合并的结果,同时限制最大数量的 对这些发布者的并发订阅。 |
<R> Flowable<R> | flatMap(Function<? super T,? extends Publisher<? extends R>> onNextMapper, Function<? super Throwable,? extends Publisher<? extends R>> onErrorMapper, Callable<? extendsPublisher<? extends R>> onCompleteSupplier) Returns a Flowable that applies a function to each item emitted or notification raised by the source Publisher and then flattens the Publishers returned from these functions and emits the resulting items. 返回一个Flowable,它将函数应用于源发布者发出的每个项目或通知,然后展平从这些函数返回的发布者并发出结果项 |
<R> Flowable<R> | flatMap(Function<? super T,? extends Publisher<? extends R>> onNextMapper,Function<Throwable,? extends Publisher<? extends R>> onErrorMapper, Callable<? extendsPublisher<? extends R>> onCompleteSupplier, int maxConcurrency) Returns a Flowable that applies a function to each item emitted or notification raised by the source Publisher and then flattens the Publishers returned from these functions and emits the resulting items, while limiting the maximum number of concurrent subscriptions to these Publishers. 返回一个Flowable,它将函数应用于源发布者发出的每个项目或通知,然后展平从这些函数返回的发布者并发出结果项,同时限制这些发布者的最大并发订阅数。 flattens:弄平,打到,这里翻译成展平是有根据的,源Publisher是线性的一个个发射,展平后的可以一起同时发射,但是要控制并发数 |
<R> Flowable<R> | flatMap(Function<? super T,? extends Publisher<? extends R>> mapper, int maxConcurrency) Returns a Flowable that emits items based on applying a function that you supply to each item emitted by the source Publisher, where that function returns a Publisher, and then merging those resulting Publishers and emitting the results of this merger, while limiting the maximum number of concurrent subscriptions to these Publishers. 返回一个Flowable,它根据您提供给源Publisher发出的每个项目的函数来发出项目,该函数返回一个Publisher,然后合并那些生成的Publishers并发出此合并的结果,同时限制最大数量的 对这些发布者的并发订阅。 |
<U,R> Flowable<R> | flatMap(Function<? super T,? extends Publisher<? extends U>> mapper, BiFunction<? superT,? super U,? extends R> combiner) Returns a Flowable that emits the results of a specified function to the pair of values emitted by the source Publisher and a specified collection Publisher. 返回一个Flowable,它将指定函数的结果发送到源Publisher和指定集合Publisher发出的对值。 |
<U,R> Flowable<R> | flatMap(Function<? super T,? extends Publisher<? extends U>> mapper, BiFunction<? superT,? super U,? extends R> combiner, boolean delayErrors) Returns a Flowable that emits the results of a specified function to the pair of values emitted by the source Publisher and a specified collection Publisher. 返回一个Flowable,它将指定函数的结果发送到源Publisher和指定集合Publisher发出的对值。 |
<U,R> Flowable<R> | flatMap(Function<? super T,? extends Publisher<? extends U>> mapper, BiFunction<? superT,? super U,? extends R> combiner, boolean delayErrors, int maxConcurrency) Returns a Flowable that emits the results of a specified function to the pair of values emitted by the source Publisher and a specified collection Publisher, while limiting the maximum number of concurrent subscriptions to these Publishers. 返回一个Flowable,它将指定函数的结果发送到源Publisher和指定集合Publisher发出的对值,同时限制这些发布者的最大并发订阅数。 |
<U,R> Flowable<R> | flatMap(Function<? super T,? extends Publisher<? extends U>> mapper, BiFunction<? superT,? super U,? extends R> combiner, boolean delayErrors, int maxConcurrency, int bufferSize) Returns a Flowable that emits the results of a specified function to the pair of values emitted by the source Publisher and a specified collection Publisher, while limiting the maximum number of concurrent subscriptions to these Publishers. 返回一个Flowable,它将指定函数的结果发送到源Publisher和指定集合Publisher发出的对值,同时限制这些发布者的最大并发订阅数。 |
<U,R> Flowable<R> | flatMap(Function<? super T,? extends Publisher<? extends U>> mapper, BiFunction<? superT,? super U,? extends R> combiner, int maxConcurrency) Returns a Flowable that emits the results of a specified function to the pair of values emitted by the source Publisher and a specified collection Publisher, while limiting the maximum number of concurrent subscriptions to these Publishers. 返回一个Flowable,它将指定函数的结果发送到源Publisher和指定集合Publisher发出的对值,同时限制这些发布者的最大并发订阅数。 |
Completable | flatMapCompletable(Function<? super T,? extends CompletableSource> mapper) Maps each element of the upstream Flowable into CompletableSources, subscribes to them and waits until the upstream and all CompletableSources complete. 将上游Flowable的每个元素映射到CompletableSources,订阅它们并等待上游和所有CompletableSources完成。 |
Completable | flatMapCompletable(Function<? super T,? extends CompletableSource> mapper, boolean delayErrors, int maxConcurrency) Maps each element of the upstream Flowable into CompletableSources, subscribes to them and waits until the upstream and all CompletableSources complete, optionally delaying all errors. 将上游Flowable的每个元素映射到CompletableSources,订阅它们并等待上游和所有CompletableSources完成,可选地延迟所有错误。 |
<U> Flowable<U> | flatMapIterable(Function<? super T,? extends Iterable<? extends U>> mapper) Returns a Flowable that merges each item emitted by the source Publisher with the values in an Iterable corresponding to that item that is generated by a selector. 返回一个Flowable,它将源Publisher发出的每个项与Iterable中与选择器生成的项对应的值合并。 |
<U,V> Flowable<V> | flatMapIterable(Function<? super T,? extends Iterable<? extends U>> mapper, BiFunction<? super T,? super U,? extends V> resultSelector) Returns a Flowable that emits the results of applying a function to the pair of values from the source Publisher and an Iterable corresponding to that item that is generated by a selector. 返回一个Flowable,它发出将函数应用于源Publisher中的值对和对应于由选择器生成的该项的Iterable的结果。 |
<U,V> Flowable<V> | flatMapIterable(Function<? super T,? extends Iterable<? extends U>> mapper, BiFunction<? super T,? super U,? extends V> resultSelector, int prefetch) Returns a Flowable that merges each item emitted by the source Publisher with the values in an Iterable corresponding to that item that is generated by a selector, while limiting the number of concurrent subscriptions to these Publishers. 返回一个Flowable,它将源Publisher发出的每个项目与Iterable中与选择器生成的项目相对应的值合并,同时限制这些Publishers的并发订阅数量。 |
<U> Flowable<U> | flatMapIterable(Function<? super T,? extends Iterable<? extends U>> mapper, int bufferSize) Returns a Flowable that merges each item emitted by the source Publisher with the values in an Iterable corresponding to that item that is generated by a selector. 返回一个Flowable,它将源Publisher发出的每个项与Iterable中与选择器生成的项对应的值合并。 |
<R> Flowable<R> | flatMapMaybe(Function<? super T,? extends MaybeSource<? extends R>> mapper) Maps each element of the upstream Flowable into MaybeSources, subscribes to all of them and merges their onSuccess values, in no particular order, into a single Flowable sequence. 将上游Flowable的每个元素映射到MaybeSources,订阅所有元素并将其onSuccess值以无特定顺序合并为单个Flowable序列。 |
<R> Flowable<R> | flatMapMaybe(Function<? super T,? extends MaybeSource<? extends R>> mapper, boolean delayErrors, int maxConcurrency) Maps each element of the upstream Flowable into MaybeSources, subscribes to at most 将上游Flowable的每个元素映射到MaybeSources,一次最多订阅maxConcurrencyMaybeSources,并将其onSuccess值以无特定顺序合并为单个Flowable序列,可选地延迟所有错误。 |
<R> Flowable<R> | flatMapSingle(Function<? super T,? extends SingleSource<? extends R>> mapper) Maps each element of the upstream Flowable into SingleSources, subscribes to all of them and merges their onSuccess values, in no particular order, into a single Flowable sequence. 将上游Flowable的每个元素映射到SingleSources,订阅所有元素并将其onSuccess值以无特定顺序合并为单个Flowable序列。 |
<R> Flowable<R> | flatMapSingle(Function<? super T,? extends SingleSource<? extends R>> mapper, boolean delayErrors, int maxConcurrency) Maps each element of the upstream Flowable into SingleSources, subscribes to at most 将上游Flowable的每个元素映射到SingleSources,一次最多订阅maxConcurrency SingleSources,并将其onSuccess值以无特定顺序合并为单个Flowable序列,可选地延迟所有错误。 |
3 重点单词
flattens:弄平;打到;展平
这里翻译成展平是有根据的,源Publisher是线性的一个个发射,展平后的可以一起同时发射,但是要控制并发数
4 flatMap图解和说明
将不同颜色的圆形根据指定的规则转换成方形,这个规则是可以自己定制的

5 flatMap测试用例
这里只是使用了最简单的用例,也就是根据原有的发射,通过指定规则转换为一个新的Publisher,然后将这些新生成的子Publisher规整到一起,按顺序一一发射这些新的Publisher的数据,如下面将每一个项加了“ = 当前时间”。
@Test
public void flatMap() {
System.out.println("######flatMap#####");
Flowable.just("item1", "item2", "item3").flatMap(new Function<String, Publisher<String>>() {
@Override
public Flowable<String> apply(String s) throws Exception {
return Flowable.just(s + " = " + System.currentTimeMillis());
}
}).subscribe(new Consumer<String>() {
@Override
public void accept(String value) throws Exception {
System.out.println(value);
}
});
}
测试结果
######flatMap#####
item1 = 1538888185993
item2 = 1538888185993
item3 = 1538888185993
6 flatMapIterable
flatMapIterable与flatMap是类似的,不同的是flatMapIterable把每一个元素转换成Iterable,
7 flatMapMaybe
flatMapMaybe与flatMap是类似的,也是根据指定的Function将将上游Flowable的每个元素根据新规则生成MaybeSources,订阅所有元素并将其onSuccess值以无特定顺序合并为单个Flowable序列。
8 flatMapSingle
flatMapSingle与flatMap是类似的,也是根据指定的Function将将上游Flowable的每个元素根据新规则生成SingleSources
本文深入探讨RxJava2中的flatMap家族,包括flatMap、flatMapIterable、flatMapMaybe和flatMapSingle。详细阐述它们的作用、使用场景、接口定义、并发控制及各种用例,帮助理解如何将不同数据源转换和整合。
1571

被折叠的 条评论
为什么被折叠?



