How to Convert Mono<List<T>> Into Flux<T> – 如何将单声道<列表<T>>转换为流动<T&gt

最后修改: 2021年 5月 12日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Sometimes in Reactive Programming, we could have a publisher of a large collection of items. In some cases, consumers of this publisher might not be able to process all items in one go. Therefore, we may need to publish each item asynchronously to match the consumer’s processing speed.

反应式编程中,有时我们可能有一个大量项目集合的发布者。在某些情况下,该发布者的消费者可能无法一次性处理所有项目。因此,我们可能需要异步发布每个项目,以配合消费者的处理速度。

In this tutorial, we’ll be looking into some ways by which we can convert our Mono of a Collection to Flux of Collection’s items.

在本教程中,我们将研究一些方法,通过这些方法,我们可以将CollectionMono转换为Collection的项的Flux

2. Problem Description

2.问题描述

When working with Reactive Streams, we use a Publisher and its two implementations, Flux and Mono. Though Mono<T> is a type of Publisher<T> that can emit 0 or 1 item of type T, the Flux<T> can emit 0 to N items of type T.

在使用 Reactive Streams 时,我们使用 Publisher 及其两个实现,FluxMono.尽管Mono<T>Publisher<T>的一种类型,可以发出0或1个T类型的项目,但Flux<T>可以发出0到N类型的T>项目。

Let’s say we have a Mono publisher that is holding a Mono<List<T>> — an iterable collection of items of type T. Our requirement is to produce the collection items asynchronously using Flux<T>:

假设我们有一个Mono发布器,它持有一个Mono<List<T>>/em>–一个可迭代的T类型的项目集合。我们的要求是使用Flux<T>异步产生集合项:

Here, we can see that we need an operator on Mono<List<T>> that can perform this transformation. First, we’ll extract collection items from stream publisher Mono and then produce items one by one asynchronously as Flux.

在这里,我们可以看到我们需要一个在Mono<List<T>>上的操作符,可以执行这种转换。首先,我们将从流发布者Mono中提取集合项,然后以Flux的方式一个一个地异步产生。

The Mono publisher contains a map operator, which can transform a Mono synchronously, and a flatMap operator for transforming a Mono asynchronously. Also, both of these operators produce a single item as an output.

Mono发布器包含一个map操作符,它可以同步地转换一个Mono,以及一个flatMap操作符,用于异步地转换一个Mono。而且,这两个操作符都会产生一个单项作为输出。

However, for our use case to produce many items after flattening Mono<List<T>>, we can use flatMapMany or flatMapIterable.

然而, 对于我们的用例来说,在扁平化Mono<List<T>>之后产生许多项,我们可以使用flatMapMany或者flatMapIterable

Let’s explore how to use these operators.

让我们来探讨如何使用这些运算符。

3. flatMapMany

3.flatMapMany

Let’s start with a sample List of String to create our Mono publisher:

让我们从一个ListString样本开始,创建我们的Mono发布器。

private Mono<List<String>> monoOfList() {
    List<String> list = new ArrayList<>();
    list.add("one");
    list.add("two");
    list.add("three");
    list.add("four");

    return Mono.just(list);
}

The flatMapMany is a generic operator on Mono that returns a Publisher. Let’s apply flatMapManyto our solution:

flatMapMany是一个关于Mono的通用操作符,它返回一个Publisher。让我们将flatMapMany应用于我们的解决方案。

private <T> Flux<T> monoTofluxUsingFlatMapMany(Mono<List<T>> monoList) {
    return monoList
      .flatMapMany(Flux::fromIterable)
      .log();
}

In this case, the flatMapMany takes the Mono‘s List, flattens it, and creates a Flux publisher using Flux operator the fromIterable. We also used log() here to log each element produced. Therefore, this will output elements one by one like “one“, “two“, “three“, “four“, and then terminate.

在这种情况下,flatMapMany 获取MonoList,将其压扁,并使用Flux操作符Flux创建一个Flux发布器,fromIterable我们还在这里使用了log()来记录产生的每个元素。因此,这将逐一输出元素,如”one“、”two“、”three“、”four“,然后终止

4. flatMapIterable

4.flatMapIterable

For the same sample List of String, we’ll now explore flatMapIterable — a custom-built operator.

对于同样的ListString,我们现在将探索flatMapIterable–一个自定义的操作符。

Here, we don’t need to create Flux explicitly from the List; we only need to provide the List. This operator implicitly creates a Flux out of its elements. Let’s use flatMapIterable for our solution:

在这里,我们不需要从List明确地创建Flux;我们只需要提供List。这个操作符隐含地从其元素中创建了一个Flux。让我们使用flatMapIterable来解决。

private <T> Flux<T> monoTofluxUsingFlatMapIterable(Mono<List<T>> monoList) {
    return monoList
      .flatMapIterable(list -> list)
      .log();
}

Here, flatMapIterable takes the Mono‘s List and converts it internally into a Flux of its elements. Hence, it’s more optimized compared with the flatMapMany operator. And this will output the same “one“, “two“, “three“, “four“, and then terminate.

在这里, flatMapIterable获取MonoList,并在内部将其转换为其元素的Flux。因此,与flatMapMany操作符相比,它更为优化。而这将输出同样的”“、”“、”“、”“,然后终止

5. Conclusion

5.总结

In this article, we discussed different ways to convert a Mono<List<T>> into Flux<T> using the operators flatMapMany and flatMapIterable. Both are easy-to-use operators. Whereas flatMapMany is useful for more generic publishers, flatMapIterable is better optimized for such purposes.

在这篇文章中,我们讨论了使用操作符flatMapManyflatMapIterableMono<List<T>>转换成Flux<T>的不同方法。这两个都是易于使用的操作符。而flatMapMany对于更多的通用发布者是有用的,flatMapIterable则是为此类目的而更好地优化。

As always, the code example is available over on GitHub.

一如既往,代码示例在可获得在GitHub上