当前位置:网站首页>list集合根据对象某属性求和,最大值等
list集合根据对象某属性求和,最大值等
2022-07-05 15:26:00 【小蚂蚁hjk】
先初始化集合并添加数据
//实体类
public class Student {
private int mathScoresInt; //数学成绩
private long mathScoresLong;//数学成绩
private float mathScoresFloat;//数学成绩
private double mathScoresDouble;//数学成绩
private BigDecimal mathScoresBigDecimal;//数学成绩
//构造方法忽略
//set、get 方法忽略
}
//测试数据 ( 不允许list中存在为空的值,不然会异常! )
List<Student> list = new ArrayList();
list.add(new Student(87, 87, 87.5f, 87.8, new BigDecimal(87)));
list.add(new Student(88, 88, 88.5f, 88.8, new BigDecimal(88)));
list.add(new Student(89, 89, 89.5f, 89.8, new BigDecimal(89)));
list.add(new Student(90, 90, 90.5f, 90.8, new BigDecimal(90)));
提示:以下计算为int 、long 、float 、double 、BigDecimal 等类型
一、根据List中的对象的某个属性,求和
double mathAverageInt = list.stream().mapToInt( Student::getMathScoresInt ).average().orElse(0d);
double mathAverageLong = list.stream().mapToLong( Student::getMathScoresLong ).average().orElse(0d);
double mathAverageDouble = list.stream().mapToDouble( Student::getMathScoresDouble ).average().orElse(0d);
二、根据List中的对象的某个属性,求平均值
double mathAverageInt = list.stream().mapToInt( Student::getMathScoresInt ).average().orElse(0d);
double mathAverageLong = list.stream().mapToLong( Student::getMathScoresLong ).average().orElse(0d);
double mathAverageDouble = list.stream().mapToDouble( Student::getMathScoresDouble ).average().orElse(0d);
三、根据List中的对象的某个属性,求最大值
int mathMaxInt = list.stream().mapToInt( Student::getMathScoresInt ).max().getAsInt(); //int类型
long mathMaxLong = list.stream().mapToLong( Student::getMathScoresLong ).max().getAsLong();
double mathMaxDouble = list.stream().mapToDouble( Student::getMathScoresDouble ).max().getAsDouble();
BigDecimal mathMaxBigDecimal = list.stream().map( Student::getMathScoresBigDecimal ).reduce(BigDecimal.ZERO, BigDecimal::max);
四、根据List中的对象的某个属性,求最小值
int mathMinInt = list.stream().mapToInt( Student::getMathScoresInt ).min().getAsInt();
long mathMinLong = list.stream().mapToLong( Student::getMathScoresLong ).min().getAsLong();
double mathMinDouble = list.stream().mapToDouble( Student::getMathScoresDouble ).min().getAsDouble();
BigDecimal mathMinBigDecimal = list.stream().map( Student::getMathScoresBigDecimal ).reduce(BigDecimal.ZERO, BigDecimal::min);
边栏推荐
- RepLKNet:不是大卷积不好,而是卷积不够大,31x31卷积了解一下 | CVPR 2022
- Data communication foundation - Ethernet port mirroring and link aggregation
- Misc Basic test method and knowledge points of CTF
- 18.[stm32] read the ROM of DS18B20 temperature sensor and realize multi-point temperature measurement
- I spring web upload
- Calculate weight and comprehensive score by R entropy weight method
- keep-alive
- Redis' transaction mechanism
- 18.[STM32]读取DS18B20温度传感器的ROM并实现多点测量温度
- Noi / 1.5 37: mercenaries
猜你喜欢

Vulnhub-Moneybox

vlunhub- BoredHackerBlog Moriarty Corp

Number protection AXB function! (essence)

定义严苛标准,英特尔Evo 3.0正在加速PC产业升级

Anti shake and throttling
![16.[STM32]从原理开始带你了解DS18B20温度传感器-四位数码管显示温度](/img/9f/c91904b6b1d3a1e85c0b50e43972e5.jpg)
16.[STM32]从原理开始带你了解DS18B20温度传感器-四位数码管显示温度
![21.[STM32]I2C协议弄不懂,深挖时序图带你编写底层驱动](/img/f4/2c935dd9933f5cd4324c29c41ab221.png)
21.[STM32]I2C协议弄不懂,深挖时序图带你编写底层驱动

ionic cordova项目修改插件

SQL injection sqllabs (basic challenges) 11-20

Data communication foundation - Ethernet port mirroring and link aggregation
随机推荐
Summary of the third class
复现Thinkphp 2.x 任意代码执行漏洞
Codasip adds verify safe startup function to risc-v processor series
SQL injection sqllabs (basic challenges) 1-10
Data communication foundation - route republication
vlunhub- BoredHackerBlog Moriarty Corp
Transfer the idea of "Zhongtai" to the code
Detailed explanation of C language branch statements
ionic cordova项目修改插件
18.[stm32] read the ROM of DS18B20 temperature sensor and realize multi-point temperature measurement
Analytic hierarchy process of mathematical modeling (including Matlab code)
vlunhub- BoredHackerBlog Social Network
Explanation report of the explosion
verilog实现计算最大公约数和最小公倍数
lv_font_conv离线转换
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
Good article inventory
Can gbase 8A view the location of SQL statement history?
Basic JSON operations of MySQL 5.7
SQL Server learning notes