当前位置:网站首页>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);
边栏推荐
- Anti shake and throttling
- Advanced level of static and extern
- 16.[STM32]从原理开始带你了解DS18B20温度传感器-四位数码管显示温度
- JS knowledge points-01
- Data communication foundation smart_ Link_&_ Monitor_ Link
- Data communication foundation - dynamic routing protocol rip
- Reasons and solutions for redis cache penetration and cache avalanche
- Bugku easy_ nbt
- 19.[STM32]HC_SR04超声波测距_定时器方式(OLED显示)
- MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
猜你喜欢

SQL injection sqllabs (basic challenges) 11-20

ionic cordova项目修改插件

机械臂速成小指南(九):正运动学分析

Transfer the idea of "Zhongtai" to the code
![18.[STM32]读取DS18B20温度传感器的ROM并实现多点测量温度](/img/e7/4f682814ae899917c8ee981c05edb8.jpg)
18.[STM32]读取DS18B20温度传感器的ROM并实现多点测量温度

Advanced level of static and extern

一文搞定vscode编写go程序

CSRF, XSS science popularization and defense

Data communication foundation NAT network address translation

Example of lvgl display picture
随机推荐
2.3 learning content
Advanced level of static and extern
P1451 calculate the number of cells / 1329: [example 8.2] cells
Transfer the idea of "Zhongtai" to the code
Usage and usage instructions of JDBC connection pool
Bugku's eyes are not real
I include of spring and Autumn
CSDN I'm coming
lvgl 显示图片示例
The computer is busy, and the update is a little slow
Noi / 1.5 06: element maximum span value of integer sequence
I spring and autumn blasting-2
Bugku's steganography
The elimination strategy of redis
lv_ font_ Conv offline conversion
Codasip为RISC-V处理器系列增加Veridify安全启动功能
Verilog realizes the calculation of the maximum common divisor and the minimum common multiple
Codasip adds verify safe startup function to risc-v processor series
JS knowledge points-01
Example of lvgl display picture