How to convert List> to Mono>?

后端 未结 2 1151
粉色の甜心
粉色の甜心 2021-01-18 00:22

I have a method that returns Mono:

interface Processor {
  Mono process(Input input);
}

And I want

相关标签:
2条回答
  • 2021-01-18 00:45

    If you don't have to create stream for any reason, you could create Flux from your inputs, map it and collect list

    Flux.fromIterable(inputs).flatMap(processor::process).collectList();
    
    0 讨论(0)
  • 2021-01-18 01:04
    // first merge all the `Mono`s:
    List<Mono<Output>> outputs = ...
    Flux<Output> merged = Flux.empty();
    for (Mono<Output> out : outputs) {
        merged = merged.mergeWith(out);
    }
    
    // then collect them
    return merged.collectList();
    

    or (inspired by Alexander's answer)

    Flux.fromIterable(outputs).flatMap(x -> x).collectList();
    
    0 讨论(0)
提交回复
热议问题