当前位置:网站首页>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);
边栏推荐
- 机械臂速成小指南(九):正运动学分析
- vlunhub- BoredHackerBlog Moriarty Corp
- Record the pits encountered in the raspberry pie construction environment...
- 基于OpenHarmony的智能金属探测器
- Why should we learn mathematical modeling?
- 记录一下树莓派搭建环境中遇到的坑。。。
- Basic JSON operations of MySQL 5.7
- 20.[STM32]利用超声波模块和舵机实现智能垃圾桶功能
- Ten billion massage machine blue ocean, difficult to be a giant
- F. Weights assignment for tree edges problem solving Report
猜你喜欢
CODING DevSecOps 助力金融企业跑出数字加速度
"Sequelae" of the withdrawal of community group purchase from the city
Lesson 4 knowledge summary
Your childhood happiness was contracted by it
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
[brief notes] solve the problem of IDE golang code red and error reporting
Bugku's eyes are not real
Common MySQL interview questions
keep-alive
Quick completion guide for manipulator (IX): forward kinematics analysis
随机推荐
一文搞定vscode编写go程序
数学建模之层次分析法(含MATLAB代码)
Appium automation test foundation - appium basic operation API (II)
Common MySQL interview questions
Value series solution report
Noi / 1.5 06: element maximum span value of integer sequence
Basic JSON operations of MySQL 5.7
verilog实现计算最大公约数和最小公倍数
Redis' transaction mechanism
I include of spring and Autumn
JS topic - console log()
MySQL5.7的JSON基本操作
【簡記】解决IDE golang 代碼飄紅報錯
Transfer the idea of "Zhongtai" to the code
17. [stm32] use only three wires to drive LCD1602 LCD
JS knowledge points-01
Data communication foundation smart_ Link_&_ Monitor_ Link
基于OpenHarmony的智能金属探测器
Good article inventory
Number protection AXB function! (essence)