一级标题计算java实体列表中某一字段的和
使用java中自带的Collectors.summarizingxxx
首先新建实体
public class ProgramPower {
private String id;
private Double count;
##赋值
List<ProgramPower> list=new ArrayList();
ProgramPower power=new ProgramPower();
power.setCount(0.9);
list.add(power);
ProgramPower power1=new ProgramPower();
power1.setCount(0.9);
list.add(power1);
ProgramPower power2=new ProgramPower();
power2.setCount(0.9);
list.add(power2);
System.out.println(list.stream().collect(Collectors.summarizingDouble(ProgramPower::getCount)));
##打印结果
DoubleSummaryStatistics{count=3, sum=2.700000, min=0.900000, average=0.900000, max=0.900000}
具体的值可以具体获得,如和可以使用getSum():
list.stream().collect(Collectors.summarizingDouble(ProgramPower::getCount)).getSum()