Java 8 弹簧反应器组集合为一组

Java 8 弹簧反应器组集合为一组,java-8,java-stream,reactive-programming,spring-webflux,project-reactor,Java 8,Java Stream,Reactive Programming,Spring Webflux,Project Reactor,我正在试图找出如何从Flux获取Mono 如果订单包含Set 我一直在努力阅读,这里是我能得到的最接近的,但它仍然无法编译。有人能帮忙吗。在下面的示例中,orderService.getAll(orderCriteria)返回Flux final Mono customerSetMono=orderService .getAll(订单标准) .map(订单->订单.getCustomers()) .collect(Collectors.toSet())//Mono customerSet.str

我正在试图找出如何从
Flux
获取
Mono

如果订单包含
Set

我一直在努力阅读,这里是我能得到的最接近的,但它仍然无法编译。有人能帮忙吗。在下面的示例中,orderService.getAll(orderCriteria)返回
Flux

final Mono customerSetMono=orderService
.getAll(订单标准)
.map(订单->订单.getCustomers())
.collect(Collectors.toSet())//Mono customerSet.stream()
.flatMap(客户->客户.stream())
.collect(收集器.toSet());

列表
场景中,您面临的是一个
列表
,但处于被动环境中。因此,您只需使用适当的
flatMap
。下面是您的代码的外观

orderService.getAll(orderCriteria)//流量
.FlatMapiteTable(订单::getCustomers)//Flux
.collect(收集器.toSet());//单声道

orderService.getAll(orderCriteria)返回什么流?看起来应该是
.flatMap(order->order.getCustomers().stream())
。因此getAll()返回Orderfinal Mono>customerSetMono=orderService.getAll(orderCriteria)。flatMap(order->order.getCustomers().stream()).collect(collector.toSet());也没有编译
orderService.getAll(orderCriteria).map(order->order.getCustomers()).flatMapIterable(customers->customers).collect(collector.toSet())
更简短的格式-
orderService.getAll(orderCriteria).flatMapIterable(order::getCustomers()).collector(collector.toSet())
谢谢你能解释一下为什么我们不能使用这种方式吗
final Mono<Set<Customer>> customerSetMono = orderService
              .getAll(orderCriteria)
              .map(order -> order.getCustomers())
              .collect(Collectors.toSet()) //Mono<Set<Set<Customer>>
              .flatMap(
                  customerSet -> customerSet.stream()
                      .flatMap(customers -> customers.stream()))
              .collect(Collectors.toSet());