gpt4 book ai didi

java - 减少 react 器中的 GroupedFlux

转载 作者:行者123 更新时间:2023-12-02 02:36:03 26 4
gpt4 key购买 nike

我有一个内部具有不同对象类型的通量,我希望按类型对元素进行分组,然后减少每个组。

这是执行此操作的简单代码。

    Flux<Item> item=Flux.just(new Item("item1"), new Item("item2"));
Flux<Item2> item2=Flux.just(new Item2(4.0f), new Item2(2.0f));

Flux<Object> objects=Mono.empty().concatWith(item).concatWith(item2);
Flux<GroupedFlux<Object>> groups=objects.groupBy(value -> value.getClass());

如何按类型减少每个流量?在此处输入代码

最佳答案

我在gitter上找到了解决方案。感谢@simonbasle 和@osi 的重新激活。

https://gitter.im/reactor/reactor

关键是 flatMap 方法,并使用键来区分值。

groups.flatMap(gf -> {
if(gf.key()==Item.class) {
return gf.reduce(new Item(""), (a, b) -> {
return new Item(a.getValue()+((Item)b).getValue());
});
}
else if(gf.key()==Item2.class) {
return gf.reduce(new Item2(0.0f), (a, b) -> {
return new Item2(a.getValueF()+((Item2)b).getValueF());
});
}
throw new IllegalArgumentException("unknown key: "+gf.key());
});

关于java - 减少 react 器中的 GroupedFlux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46307409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com