假设我有一个信息库findAll()
返回一个方法Iterable
的State
,这里State
是代表有两个字段(的getter / setter方法)美国的一个州一个类:name
和population
。
我想人口领域的总和都State
在我的助焊剂秒。我创建从可迭代的助熔剂,如下所示:
Flux f = Flux.fromIterable(stateRepo.findAll());
我有我的流量,但我不知道的一个很好的方式来总结自己的价值观。我已经试过类似
int total = 0;
f.map(s -> s.getPopulation()).subscribe(p -> total += v);
return total;
但是,编译说总“应该是最终或有效决赛”。添加final
显然是行不通的,因为我想给它添加。
我该如何去有关通量求和(或任何其他聚合函数)?
使用减少方法:
@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] 删除。
我来说两句