当前位置:网站首页>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);
边栏推荐
- Maximum common subsequence
- 19.[STM32]HC_SR04超声波测距_定时器方式(OLED显示)
- Reasons and solutions for redis cache penetration and cache avalanche
- Data communication foundation NAT network address translation
- Ionic Cordova project modification plug-in
- OceanBase社区版之OBD方式部署方式本地安装
- CSRF, XSS science popularization and defense
- Can gbase 8A view the location of SQL statement history?
- 力扣今日题-729. 我的日程安排表 I
- String modification problem solving Report
猜你喜欢

Bugku alert

Six common transaction solutions, you sing, I come on stage (no best, only better)

Bugku cyberpunk

通过的英特尔Evo 3.0整机认证到底有多难?忆联科技告诉你

Example project: simple hexapod Walker
![[brief notes] solve the problem of IDE golang code red and error reporting](/img/b6/0b2ea06eb5fbe651ff9247b109fa15.png)
[brief notes] solve the problem of IDE golang code red and error reporting
![P6183 [USACO10MAR] The Rock Game S](/img/f4/d8c8763c27385d759d117b515fbf0f.png)
P6183 [USACO10MAR] The Rock Game S

swiper. JS to achieve barrage effect

Creation and optimization of MySQL index

【簡記】解决IDE golang 代碼飄紅報錯
随机推荐
Bugku's eyes are not real
Arduino controls a tiny hexapod 3D printing robot
P6183 [USACO10MAR] The Rock Game S
The elimination strategy of redis
Quick completion guide for manipulator (IX): forward kinematics analysis
Go language programming specification combing summary
Clock switching with multiple relationship
Database learning - Database Security
20.[STM32]利用超声波模块和舵机实现智能垃圾桶功能
go语言编程规范梳理总结
vlunhub- BoredHackerBlog Social Network
一文搞定vscode编写go程序
Memo 00
lvgl 显示图片示例
具有倍数关系的时钟切换
Appium automation test foundation - appium basic operation API (I)
Common MySQL interview questions
Maximum common subsequence
queryRunner. Query method
【簡記】解决IDE golang 代碼飄紅報錯