当前位置:网站首页>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);
边栏推荐
- Data communication foundation - routing communication between VLANs
- Nine hours, nine people, nine doors problem solving Report
- Common MySQL interview questions (1) (written MySQL interview questions)
- 具有倍数关系的时钟切换
- Vulnhub-Moneybox
- Lesson 4 knowledge summary
- Anti shake and throttling
- Example of lvgl display picture
- Common redis data types and application scenarios
- lv_font_conv离线转换
猜你喜欢
Number protection AXB function! (essence)
数学建模之层次分析法(含MATLAB代码)
21.[STM32]I2C协议弄不懂,深挖时序图带你编写底层驱动
Appium自动化测试基础 — APPium基础操作API(二)
Data communication foundation - Ethernet port mirroring and link aggregation
把 ”中台“ 的思想迁移到代码中去
Common PHP interview questions (1) (written PHP interview questions)
Verilog realizes the calculation of the maximum common divisor and the minimum common multiple
F. Min cost string problem solving Report
Bugku alert
随机推荐
Huiyuan, 30, is going to have a new owner
Appium automation test foundation - appium basic operation API (II)
Hongmeng system -- Analysis from the perspective of business
Good article inventory
我们为什么要学习数学建模?
Redis distributed lock principle and its implementation with PHP (1)
Quick completion guide for manipulator (IX): forward kinematics analysis
Value series solution report
Noi / 1.5 06: element maximum span value of integer sequence
Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
mapper. Comments in XML files
一文搞定vscode编写go程序
Optional parameters in the for loop
Ionic Cordova project modification plug-in
OSI 七层模型
Write a go program with vscode in one article
20.[STM32]利用超声波模块和舵机实现智能垃圾桶功能
I spring and autumn blasting-2
keep-alive
Calculate weight and comprehensive score by R entropy weight method