java 如何消耗焊剂内的焊剂元素?

iaqfqrcu  于 2023-01-15  发布在  Java
关注(0)|答案(1)|浏览(96)

我的密码:

Flux<A> outsideFlux = groupedFlux.map(element -> {

        //some operations that convert element to A

        Flux<Double> insideFlux = someOtherCallThatReturnsThisFluxOfDouble;

        insideFlux.subscribe(val -> A.setVal(val));
      }
    )

上面的问题是,即使在执行A.setVal(val)之后,它仍然只为空(因为值只存在于流中,然后死亡)。

xzlaal3s

xzlaal3s1#

很难知道你到底想要实现什么,但假设:

  • 是一个
  • someOtherCallThatReturnsThisFluxOfDouble是一个Flux<Double>
  • outsideFluxFlux<A>

看看下面的代码片段,它应该会对您有所帮助:

public static void main(String[] args) {
    Flux<Integer> groupedFlux = Flux.just(1, 2, 3, 4);
    Flux<A> listOfA = groupedFlux
            .map(DemoApplication::randomListOfDoubles)
            .flatMap(fluxWithDoubles -> fluxWithDoubles.collectList().flatMap(x -> Mono.just(new A(x))));
}

static class A {
    private List<Double> doubles;
    public A(List<Double> doubles) {
        this.doubles = doubles;
    }
}

private static Flux<Double> randomListOfDoubles(int number) {
    return Flux.just(100.00 + number, 200.00 + number, 300.00 + number);
}

相关问题