list.stream().mapToDouble(User::getHeight).sum()//和
list.stream().mapToDouble(User::getHeight).max()//最大
list.stream().mapToDouble(User::getHeight).min()//最小
list.stream().mapToDouble(User::getHeight).average()//平均值

当然,除了统计double类型,还有int和long

bigdecimal需要用到reduce求和

Double示例:

public class HelloWorld {
private static final DecimalFormat df = new DecimalFormat("0.00");//保留两位小数点
public static void main(String[] args) {
Random random = new Random();
List<User> list = new ArrayList<>();
for(int i=1;i<=5;i++) {
double weight = random.nextDouble() * 100 + 100;//随机身高:100-200
User u = new User(i, "用户-" + i, weight);
list.add(u);
}
System.out.println("用户:" + list);
double sum = list.stream().mapToDouble(User::getHeight).sum();
System.out.println("身高 总和:" + df.format(sum));
double max = list.stream().mapToDouble(User::getHeight).max().getAsDouble();
System.out.println("身高 最大:" + df.format(max));
double min = list.stream().mapToDouble(User::getHeight).min().getAsDouble();
System.out.println("身高 最小:" + df.format(min));
double average = list.stream().mapToDouble(User::getHeight).average().getAsDouble();
System.out.println("身高 平均:" + df.format(average)); }
private static class User{
Integer id;
String name;
double height;//身高 public User(Integer id, String name, double height) {
this.id = id;
this.name = name;
this.height = height;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public double getHeight() {
return height;
} public void setHeight(double height) {
this.height = height;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", height=" + height +
'}';
}
} }

执行结果:

用户:
[User{id=1, name='用户-1', height=192.15677342306662},
User{id=2, name='用户-2', height=196.35056058694772},
User{id=3, name='用户-3', height=101.96271958293853},
User{id=4, name='用户-4', height=110.83134063008366},
User{id=5, name='用户-5', height=106.27720636757154}]
身高 总和:707.58
身高 最大:196.35
身高 最小:101.96
身高 平均:141.52

BigDecimal示例:

public class HelloWorld {
private static final DecimalFormat df = new DecimalFormat("0.00");//保留两位小数点
public static void main(String[] args) {
Random random = new Random();
List<User> list = new ArrayList<>();
for(int i=1;i<=5;i++) {
double weight = random.nextDouble() * 100 + 100;//随机身高:100-200
list.add(new User(i, new BigDecimal(weight).setScale(BigDecimal.ROUND_HALF_UP, 2)));
}
System.out.println("list:" + list);
BigDecimal add = list.stream().map(User::getHeight).reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println("身高 总和:" + df.format(add));
Optional<User> max = list.stream().max((u1, u2) -> u1.getHeight().compareTo(u2.getHeight()));
System.out.println("身高 最大:" + df.format(max.get().getHeight()));
Optional<User> min = list.stream().min((u1, u2) -> u1.getHeight().compareTo(u2.getHeight()));
System.out.println("身高 最小:" + df.format(min.get().getHeight())); }
private static class User{
Integer id;
BigDecimal height;//身高 public User(Integer id, BigDecimal height) {
this.id = id;
this.height = height;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public BigDecimal getHeight() {
return height;
} public void setHeight(BigDecimal height) {
this.height = height;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", height=" + height +
'}';
}
} }

执行结果:

list:
[User{id=1, height=141.5472},
    User{id=2, height=133.1609},
    User{id=3, height=101.5403},
    User{id=4, height=157.8470},
    User{id=5, height=177.7596}]
身高 总和:711.8550
身高 最大:177.76
身高 最小:101.54

java8 list统计(求和、最大、最小、平均)的更多相关文章

  1. 使用streaming window函数统计用户不同时间段平均消费金额等指标

    场景 现在餐厅老板已经不满足仅仅统计历史用户消费金额总数了,他想知道每个用户半年,每个月,每天,或者一小时消费的总额,来店消费的次数以及平均金额. 给出的例子计算的是每5秒,每30秒,每1分钟的用户消 ...

  2. Linux显示内存统计最大和最小的详情

    Linux显示内存统计最大和最小的详情 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ free -l total used free shared buffe ...

  3. The Minimum Cycle Mean in a Digraph 《有向图中的最小平均权值回路》 Karp

    文件链接 Karp在1977年的论文,讲述了一种\(O(nm)\)的算法,用来求有向强连通图中最小平均权值回路(具体问题请参照这里) 本人翻译(有删改): 首先任取一个节点 \(s\) ,定义 \(F ...

  4. awk对列/行进行统计求和【转】

    场景]--类似于excel中的sum函数对列/行进行统计求和 A01 A02 A03 A09 [要求1]--对列进行统计求和 A01 A02 A03 A09 TOTAL [要求2]--对行进行统计求和 ...

  5. awk统计命令(求和、求平均、求最大值、求最小值)

    本节内容:awk统计命令 1.求和 cat data|awk '{sum+=$1} END {print "Sum = ", sum}' 2.求平均 cat data|awk '{ ...

  6. devexpress表格gridcontrol实现列统计,总计,平均,求和等。

    1.在许多项目中,经常要实现对某些列的统计.devexpress控件gridcontrol实现这些功能只需要设置某些属性,就可以达到要求了.以下例举了一个统计班级总数,人数总计,分数总计的案例.效果图 ...

  7. 水晶报表分组,统计,求和,sum()函数使用

    --Sum()函数统计的是明细所有的和 Sum(字段名) --根据分组字段统计的和 Sum ({xh_Getdinggoudan;1.Djine} ,{xh_Getdinggoudan;1.Ddgda ...

  8. 373. Find K Pairs with Smallest Sums 找出求和和最小的k组数

    [抄题]: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. D ...

  9. python spark 求解最大 最小 平均

    rdd = sc.parallelizeDoubles(testData); Now we’ll calculate the mean of our dataset.   1 LOGGER.info( ...

随机推荐

  1. Enterprise architect 类图加时序图

    原文地址:https://segmentfault.com/a/1190000005639047#articleHeader2 新建一个Project 没什么好说的,“文件-新建项目”,然后选择保存位 ...

  2. 好程序员web前端分享值得参考的css理论:OOCSS、SMACSS与BEM

    好程序员web前端分享值得参考的css理论:OOCSS.SMACSS与BEM 最近在The Sass Way里看到了Modular CSS typography一文,发现文章在开头部分就提到了OOCS ...

  3. Redis入门---字符串类型

    阅读目录 1.keys * 命令 2.判断一个键是否存在(exists key) 3.删除键 4.获取键值的数据类型 5 递增数字(incr) 6.增加指定的整数 (INCRBY) 7.减少指定的整数 ...

  4. 一款DMA性能优化记录:异步传输和指定实时信号做async IO

    关键词:DMA.sync.async.SIGIO.F_SETSIG. DMA本身用于减轻CPU负担,进行CPU off-load搬运工作. 在DMA驱动内部实现有同步和异步模式,异步模式使用dma_a ...

  5. 解决java compiler level does not match the version of the installed java project facet【转载】

    原博文地址http://blog.csdn.net/chszs/article/details/8125828 Java compiler level does not match the versi ...

  6. SpringBoot中各配置文件的优先级及加载顺序

    我们在写程序的时候会碰到各种环境(开发.测试.生产),因而,在我们切换环境的时候,我们需要手工切换配置文件的内容.这大大的加大了运维人员的负担,同时会带来一定的安全隐患. 为此,为了能更合理地重写各属 ...

  7. 终于有人把“TCC分布式事务”实现原理讲明白了!

    之前网上看到很多写分布式事务的文章,不过大多都是将分布式事务各种技术方案简单介绍一下.很多朋友看了还是不知道分布式事务到底怎么回事,在项目里到底如何使用. 所以这篇文章,就用大白话+手工绘图,并结合一 ...

  8. Oracle通过Navicat导入表数据与机构,数据无法直接查询,需要加双引号的问题

    使用navicat 导入表到ORACLE时,总是会遇到虽然表格完整导入到数据库,但是往往查不出来数据,网上提供的解决办法是把查询的列 加上 双引号,或者表名加上双引号,但这解决办法却减慢了编写sql ...

  9. Windows 支持 OpenSSH 了!

    从 Win10 1809 和 Windows Server 2019 开始 Windows 开始支持 OpenSSH Server.本文介绍一下其基本的概念和配置方法,本文演示用的环境为 Win10 ...

  10. Golang 入门 : 数组

    数组是指一系列同一类型数据的集合.数组中包含的每个数据被称为数组元素(element),这种类型可以是任意的原始类型,比如 int.string 等,也可以是用户自定义的类型.一个数组包含的元素个数被 ...