How to collect Flux of Map into Mono of Map (Flux<Map<String,String>> into Mono<Map<String,String>>)?

huangapple go评论142阅读模式
英文:

How to collect Flux of Map into Mono of Map (Flux<Map<String,String>> into Mono<Map<String,String>>)?

问题

我找到了一个广泛的关于如何在Java8流中合并地图的线程,但我无法将其翻译成Webflux。

我有两个形式的Flux:

Flux<Map<String, List<String>>> fluxOne = Flux.fromIterable(
    List.of(
        Collections.singletonMap("one", List.of("one","two")),
        Collections.singletonMap("two", List.of("one","two"))));
Flux<Map<String, String>> fluxTwo = Flux.fromIterable(
    List.of(
        Collections.singletonMap("one", "two"),
        Collections.singletonMap("one", "two")));

我想通过收集其值将其转换为Mono:

Mono<Map<String, List<String>>> monoOne = fluxOne.collect(...);
Mono<Map<String, String>> monoTwo = fluxTwo.collect(...);

所以我的问题是,我该如何做到这一点?

英文:

I have found an extensive thread of how to merge maps in java8 streams, but I cannot quite translate that to Webflux.

I have two Fluxes of the form:

Flux&lt;Map&lt;String, List&lt;String&gt;&gt;&gt; fluxOne = Flux.fromIterable(
    List.of(
        Collections.singletonMap(&quot;one&quot;, List.of(&quot;one&quot;,&quot;two&quot;)),
        Collections.singletonMap(&quot;two&quot;, List.of(&quot;one&quot;,&quot;two&quot;))));
Flux&lt;Map&lt;String, String&gt;&gt; fluxTwo = Flux.fromIterable(
    List.of(
        Collections.singletonMap(&quot;one&quot;, &quot;two&quot;),
        Collections.singletonMap(&quot;one&quot;, &quot;two&quot;)));

I'd like to transform that into Mono's by collecting it's values:

Mono&lt;Map&lt;String, List&lt;String&gt;&gt;&gt; monoOne = fluxOne.collect(...);
Mono&lt;Map&lt;String, String&gt;&gt; monoTwo = fluxTwo.collect(...);

So my question, how do I do that?

答案1

得分: 0

也许这是您在寻找的:

Mono<Map<String, List<String>>> monoOne = fluxOne.collect(HashMap::new, Map::putAll);
Mono<Map<String, String>> monoTwo = fluxTwo.collect(HashMap::new, Map::putAll);
英文:

Perhaps this is what you are looking for:

Mono&lt;Map&lt;String, List&lt;String&gt;&gt;&gt; monoOne = fluxOne.collect(HashMap::new, Map::putAll);
Mono&lt;Map&lt;String, String&gt;&gt; monoTwo = fluxTwo.collect(HashMap::new, Map::putAll);

huangapple
  • 本文由 发表于 2023年3月9日 16:32:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定