BigDecimal:
BigDecimal bb =list.stream().map(Plan::getAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
int、double、long:
double max = list.stream().mapToDouble(User::getHeight).sum();
Integer
List<Integer> list = new ArrayList<>();
list.add(3);
list.add(7);
list.add(2);
IntSummaryStatistics collect = list.stream().collect(Collectors.summarizingInt(value -> value));
System.out.println(collect);
System.out.println("统计集合元素的个数:" + collect.getCount());
System.out.println("集合元素累加之和:" + collect.getSum());
System.out.println("集合中最小值:" + collect.getMax());
System.out.println("集合中最大值:" + collect.getMin());
System.out.println("集合中平均值:" + collect.getAverage());
打印结果:
IntSummaryStatistics{count=3, sum=12, min=2, average=4.000000, max=7}
统计集合元素的个数:3
集合元素累加之和:12
集合中最小值:7
集合中最大值:2
集合中平均值:4.0