如何总结一个反应堆通量流值是多少?

马克:

假设我有一个信息库findAll()返回一个方法IterableState,这里State是代表有两个字段(的getter / setter方法)美国的一个州一个类:namepopulation

我想人口领域的总和都State在我的助焊剂秒。我创建从可迭代的助熔剂,如下所示:

Flux f = Flux.fromIterable(stateRepo.findAll());

我有我的流量,但我不知道的一个很好的方式来总结自己的价值观。我已经试过类似

int total = 0;
f.map(s -> s.getPopulation()).subscribe(p -> total += v);
return total;

但是,编译说总“应该是最终或有效决赛”。添加final显然是行不通的,因为我想给它添加。

我该如何去有关通量求和(或任何其他聚合函数)?

mroman:

使用减少方法:

@GetMapping("/populations")
    public Mono<Integer> getPopulation() {
        return Flux.fromIterable(stateRepo.findAll())
                .map(s -> s.getPopulation())
                .reduce(0, (x1, x2) -> x1 + x2)
                .map(this::someFunction); // here you can handle the sum
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章