当前位置:网站首页>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);
边栏推荐
- 机械臂速成小指南(九):正运动学分析
- Go language programming specification combing summary
- Your childhood happiness was contracted by it
- Reasons and solutions for redis cache penetration and cache avalanche
- vlunhub- BoredHackerBlog Moriarty Corp
- Bubble sort, insert sort
- Data communication foundation - dynamic routing protocol rip
- swiper. JS to achieve barrage effect
- Bugku cyberpunk
- MySQL表字段调整
猜你喜欢
Data communication foundation - Ethernet port mirroring and link aggregation
机械臂速成小指南(九):正运动学分析
Example of lvgl display picture
Data communication foundation - route republication
Why should we learn mathematical modeling?
SQL injection sqllabs (basic challenges) 1-10
Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
Usage and usage instructions of JDBC connection pool
17.[STM32]仅用三根线带你驱动LCD1602液晶
【 note 】 résoudre l'erreur de code IDE golang
随机推荐
The OBD deployment mode of oceanbase Community Edition is installed locally
Creation and optimization of MySQL index
Object. defineProperty() - VS - new Proxy()
I spring and autumn blasting-1
F. Min cost string problem solving Report
Information collection of penetration test
Bugku's steganography
Quick completion guide for manipulator (IX): forward kinematics analysis
Optional parameters in the for loop
Data communication foundation NAT network address translation
Good article inventory
lv_ font_ Conv offline conversion
Data communication foundation ACL access control list
OceanBase社区版之OBD方式部署方式本地安装
Detailed explanation of QT creator breakpoint debugger
17.[STM32]仅用三根线带你驱动LCD1602液晶
基于OpenHarmony的智能金属探测器
2.3 learning content
力扣今日题-729. 我的日程安排表 I
Advanced level of static and extern