英文:
What is the proper way of zipping Flux with Mono?
问题
以下是翻译好的内容:
有一个 Mono<A>
和 Flux<B>
,我们需要创建一个类似这样的元组流:
Mono<A> monoA = createMono(); // {a}
Flux<B> fluxB = createFlux(); // {b1, b2, ... b100, ...}
Flux<Tuple<A,B>> zippedTuples = magicZip(monoA, fluxB); // { (a:b1), (a:b2), ... (a:b100), ...}
编写 magicZip
函数的正确(或标准)方法是什么?
英文:
There's a Mono<A>
and Flux<B>
, and we need to create a flux of tuples like this:
Mono<A> monoA = createMono(); // {a}
Flux<B> fluxB = createFlux(); // {b1, b2, ... b100, ...}
Flux<Tuple<A,B>> zippedTuples = magicZip(monoA, fluxB); // { (a:b1), (a:b2), ... (a:b100), ...}
What is the proper (or standard) way to write the magicZip
function?
答案1
得分: 1
你可以创建这个方法:
private <T> Flux<Tuple2<T, T>> magicZip(Mono<T> mono, Flux<T> flux) {
Flux<T> repeatableMono = mono.repeat();
return flux.zipWith(repeatableMono);
}
字符串类型的示例:
Flux<Tuple2<String, String>> test = magicZip(getMono(), getFlux()).doOnNext(objects -> System.out.println(objects.getT1() + objects.getT2()));
test.blockLast();
英文:
You can create this method:
private <T>Flux<Tuple2<T, T>> magicZip(Mono<T> mono, Flux<T> flux) {
Flux<T> repeatableMono = mono.repeat();
return flux.zipWith(repeatableMono);
}
Example for the String type:
Flux<Tuple2<String, String>> test = magicZip(getMono(), getFlux()).doOnNext(objects -> System.out.println(objects.getT1() + objects.getT2()));
test.blockLast();
答案2
得分: 0
我认为使用zip
函数是不可能的,因为它生成的元素数量与两者中最小的数量相同。
我认为你可以实现这样的方式:
Flux<A> fluxA = monoA.flux();
Flux<Tuple2<A,B>> zippedTuples = fluxB.flatMap(b -> fluxA.map(a -> Tuples.of(a,b)));
英文:
I think that with zip function is not possible because it produces as many elements as the smallest of two.
The way I think you can achive this is:
Flux<A> fluxA = monoA.flux();
Flux<Tuple2<A,B>> zippedTuples =fluxB.flatMap(b -> fluxA.map(a -> Tuples.of(a,b)));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论