当前位置:网站首页>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);
边栏推荐
- Ten billion massage machine blue ocean, difficult to be a giant
- Huawei Hubble incarnation hard technology IPO harvester
- Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
- Example of lvgl display picture
- queryRunner. Query method
- 一文搞定vscode编写go程序
- 【簡記】解决IDE golang 代碼飄紅報錯
- 如何将 DevSecOps 引入企业?
- Install PHP extension spoole
- lv_font_conv离线转换
猜你喜欢
Quick completion guide for manipulator (IX): forward kinematics analysis
Data communication foundation - route republication
Example of lvgl display picture
Database learning - Database Security
Common PHP interview questions (1) (written PHP interview questions)
【简记】解决IDE golang 代码飘红报错
Bugku easy_ nbt
19.[STM32]HC_SR04超声波测距_定时器方式(OLED显示)
Verilog realizes the calculation of the maximum common divisor and the minimum common multiple
Information collection of penetration test
随机推荐
Example project: simple hexapod Walker
Nine hours, nine people, nine doors problem solving Report
CODING DevSecOps 助力金融企业跑出数字加速度
Bugku cyberpunk
把 ”中台“ 的思想迁移到代码中去
OSI 七层模型
String modification problem solving Report
Array sorting num ranking merge in ascending order
我们为什么要学习数学建模?
Reasons and solutions for redis cache penetration and cache avalanche
episodic和batch的定义
How difficult is it to pass the certification of Intel Evo 3.0? Yilian technology tells you
Codasip为RISC-V处理器系列增加Veridify安全启动功能
Optional parameters in the for loop
21. [STM32] I don't understand the I2C protocol. Dig deep into the sequence diagram to help you write the underlying driver
D-snow halo solution
Lesson 4 knowledge summary
I spring web upload
Verilog realizes the calculation of the maximum common divisor and the minimum common multiple
lvgl 显示图片示例