当前位置:网站首页>The list set is summed up according to a certain attribute of the object, the maximum value, etc
The list set is summed up according to a certain attribute of the object, the maximum value, etc
2022-07-05 15:58:00 【Ant HJK】
First initialize the collection and add data
// Entity class
public class Student {
private int mathScoresInt; // Math scores
private long mathScoresLong;// Math scores
private float mathScoresFloat;// Math scores
private double mathScoresDouble;// Math scores
private BigDecimal mathScoresBigDecimal;// Math scores
// Construction method ignored
//set、get Method ignored
}
// Test data ( Don't allow list There is a null value in , Otherwise, it will be abnormal ! )
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)));
Tips : The following calculation is int 、long 、float 、double 、BigDecimal Other types
One 、 according to List A property of an object in , Sum up
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);
Two 、 according to List A property of an object in , averaging
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);
3、 ... and 、 according to List A property of an object in , For maximum
int mathMaxInt = list.stream().mapToInt( Student::getMathScoresInt ).max().getAsInt(); //int type
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);
Four 、 according to List A property of an object in , For the minimum
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);
边栏推荐
- wyt 。。
- 对象和类的关系
- I'm fat, huh
- Data communication foundation - Ethernet port mirroring and link aggregation
- episodic和batch的定义
- Research and practice of super-resolution technology in the field of real-time audio and video
- 20. [stm32] realize the function of intelligent garbage can by using ultrasonic module and steering gear
- Definition of episodic and batch
- 17. [stm32] use only three wires to drive LCD1602 LCD
- verilog实现计算最大公约数和最小公倍数
猜你喜欢
vulnhub-FirstBlood
list去重并统计个数
[Netease Yunxin] research and practice of super-resolution technology in the field of real-time audio and video
Arduino controls a tiny hexapod 3D printing robot
D-snow halo solution
CSRF, XSS science popularization and defense
【网易云信】超分辨率技术在实时音视频领域的研究与实践
Verilog realizes the calculation of the maximum common divisor and the minimum common multiple
MySQL overview
项目中批量update
随机推荐
Data communication foundation - Ethernet port mirroring and link aggregation
abstract关键字和哪些关键字会发生冲突呢
sql中set标签的使用
Arduino控制微小的六足3D打印机器人
F. Weights assignment for tree edges problem solving Report
Explanation report of the explosion
MySQL overview
Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
Vulnhub-Moneybox
Subclasses and superclasses of abstract classes
MySQL table field adjustment
Basic JSON operations of MySQL 5.7
把 ”中台“ 的思想迁移到代码中去
CODING DevSecOps 助力金融企业跑出数字加速度
MySQL 巨坑:update 更新慎用影响行数做判断!!!
【简记】解决IDE golang 代码飘红报错
我们为什么要学习数学建模?
Optional parameters in the for loop
Xiao Sha's arithmetic problem solving Report
Dataarts studio data architecture - Introduction to data standards