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. token

    18f9nWvThC274lo3USjgfeldynt0t/r/w0yjLbj9 http://app-static.acc5.com/app/testpost.php

  2. Redis原理

    RESP协议 支持tcp协议.基本数据类型,比如数组,字符串等,也可支持其他的通信场景. 模拟redis接收传输过来的set数据 //ServerSocket监听6379端口模拟redis publi ...

  3. python list 中 remove 的骚操作/易错点

    在过去的某一天(2019.3.19),有个学弟问了一个关于python list中的一个问题: 比如我们已知一个列表 [3,4,5,6,5,4,3] 我们想删除第一个为3的元素. 我们尝试了如下几种方 ...

  4. 关于B树B+树的详细解释——绝对精彩

    B树是一种完全平衡树,B+树是B树的升级版,使用更多.B树和B+树存在的目的是如何提高磁盘文件的访问(如数据库)效率. 关于B树和B+树的一篇比较好的文章: https://www.cnblogs.c ...

  5. asp.net core源码地址

    https://github.com/dotnet/corefx 这个是.net core的 开源项目地址 https://github.com/aspnet 这个下面是asp.net core 框架 ...

  6. mysql千万级数据优化查询

    我们在做一个项目,一个网站或一个app时,用户量巨增,当使用的mysql数据库中的表数据达到千万级时,可以从以下方面考滤优化: 1.在设计数据库表的时候就要考虑到优化 2.查询sql语句上的优化 3. ...

  7. python+opencv读取视频,调用摄像头

    引用 import cv2 import numpy 创建摄像头对象 cap = cv2.VideoCapture("videoTest/test1.mp4") #参数为视频文件目 ...

  8. c语言提高第二天

    一.指针强化1.指针也是一种数据类型,指针变量也是一种变量,和int a本质是一样的 1)指针变量也是一种变量,也有空间,32位程序大小为4个字节 int *p = 0x1122; 2)*操作符,*相 ...

  9. python json数据的转换

    1  Python数据转json字符串 import json json_str = json.dumps(py_data) 参数解析: json_str = json.dumps(py_data,s ...

  10. Spring MVC 使用介绍(十)—— 编码

    一.概述 客户端与服务器端在交互过程中,需要将字符以某种编码方式转化为字节流进行传输,因此涉及字符的编码和解码.某一方以编码方案A编码,另一方须以同样的编码方案解码,否则会出现乱码. 客户端与服务器端 ...