1、对BigDecimal类型的值求和。
List<Map<String,Object>> list = new ArrayList<>();
Map<String,Object> stu1 = new HashMap<String, Object>();
stu1.put("name", "张三");
stu1.put("score", new BigDecimal(80));
list.add(stu1);
Map<String,Object> stu2 = new HashMap<String, Object>();
stu2.put("name", "李斯");
stu2.put("score", new BigDecimal(90));
list.add(stu2);
Map<String,Object> stu3 = new HashMap<String, Object>();
stu3.put("name", "王武");
stu3.put("score", new BigDecimal(60));
list.add(stu3);
BigDecimal sum = list.stream().map(
x -> new BigDecimal(x.get("score").toString())
).reduce(BigDecimal.ZERO,BigDecimal::add);
System.out.println("三人总分:"+sum);
2、对double型的值求和。
List<DisProductPo> list2 = new ArrayList<>();
DisProductPo p1 = new DisProductPo();
p1.setPrintvalue(120);
p1.setViewspotticketname("产品1");
list2.add(p1);
DisProductPo p2 = new DisProductPo();
p2.setPrintvalue(80);
p2.setViewspotticketname("产品2");
list2.add(p2);
DisProductPo p3 = new DisProductPo();
p3.setPrintvalue(50);
p3.setViewspotticketname("产品3");
list2.add(p3);
double sum1 = list2.stream().collect(Collectors.summingDouble(x -> ((Double) x.getPrintvalue())));
double sum2 = list2.stream().mapToDouble(DisProductPo::getPrintvalue).sum();
System.out.println("总价格:"+sum1+"--"+sum2);