Java 如何将 Mono<List<String>> 转换为 Flux<String>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42007841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert Mono<List<String>> into Flux<String>
提问by Mavo
I'm converting small project written in RxJava 1.x to Reactor 3.x. All is good, except that I could not find out how to replace flatMap(Observable::from)
with an appropriate counterpart. I have Mono<List<String>>
and I need to convert it to Flux<String>
.
我正在将用 RxJava 1.x 编写的小项目转换为 Reactor 3.x。一切都很好,只是我不知道如何flatMap(Observable::from)
用合适的对应物替换。我有Mono<List<String>>
,我需要将其转换为Flux<String>
.
采纳答案by Simon Baslé
In Reactor 3, the from
operator has been specialized into a few variants, depending on the original source (array, iterable, etc...).
在 Reactor 3 中,from
运算符被专门化为几个变体,具体取决于原始来源(数组、可迭代等...)。
Use yourMono.flatMapMany(Flux::fromIterable)
in your case.
yourMono.flatMapMany(Flux::fromIterable)
在你的情况下使用。
回答by Anthony Pi?ero
Thanks Simon, I implemented something like this:
谢谢西蒙,我实现了这样的事情:
List<Object> dbObjects = ListObjectsBD();
List<Dao> daos = mapperObjToDao(dbObjects);
Flux<Dao> daoFlux = Mono.just(daos).flatMapMany(Flux::fromIterable);
回答by Michail Alexakis
I think that probably Flux::mergeSequential
static factory fits better here:
我认为Flux::mergeSequential
静态工厂可能更适合这里:
Iterable<Mono<String>> monos = ...
Flux<String> f = Flux.mergeSequential(monos);
This kind of merge (sequential) will maintain the ordering inside given source iterable, and will also subscribe/request eagerly from all participating sources (so more parallelization expected while computing mono results).
这种合并(顺序)将保持给定源可迭代内的排序,并且还会从所有参与的源急切地订阅/请求(因此在计算单声道结果时需要更多的并行化)。