当前位置:网站首页>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 learning ----- relevant knowledge of JWT
- 1330: [example 8.3] minimum steps
- 一文搞定vscode编写go程序
- Data communication foundation ACL access control list
- MySQL5.7的JSON基本操作
- Your childhood happiness was contracted by it
- Misc Basic test method and knowledge points of CTF
- verilog实现计算最大公约数和最小公倍数
- Quick completion guide for manipulator (IX): forward kinematics analysis
- MySQL overview
猜你喜欢

wxml2canvas

Database learning - Database Security

Stop B makes short videos, learns Tiktok to die, learns YouTube to live?

Bubble sort, insert sort

定义严苛标准,英特尔Evo 3.0正在加速PC产业升级

Data communication foundation - route republication

【 note 】 résoudre l'erreur de code IDE golang

OSI seven layer model

Aike AI frontier promotion (7.5)
![1330: [example 8.3] minimum steps](/img/69/9cb13ac4f47979b498fa2254894ed1.gif)
1330: [example 8.3] minimum steps
随机推荐
F. Weights assignment for tree edges problem solving Report
18.[STM32]读取DS18B20温度传感器的ROM并实现多点测量温度
21. [STM32] I don't understand the I2C protocol. Dig deep into the sequence diagram to help you write the underlying driver
Bugku's Eval
Linear DP (basic questions have been updated)
Number protection AXB function! (essence)
Go learning ----- relevant knowledge of JWT
Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
Appium自动化测试基础 — APPium基础操作API(二)
Data communication foundation NAT network address translation
Noi / 1.4 07: collect bottle caps to win awards
I spring and autumn blasting-1
MySQL表字段调整
Common MySQL interview questions (1) (written MySQL interview questions)
Hongmeng system -- Analysis from the perspective of business
OceanBase社区版之OBD方式部署方式本地安装
Huawei Hubble incarnation hard technology IPO harvester
17.[STM32]仅用三根线带你驱动LCD1602液晶
F. Min cost string problem solving Report
机械臂速成小指南(九):正运动学分析