当前位置:网站首页>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);
边栏推荐
- Virtual base class (a little difficult)
- Defining strict standards, Intel Evo 3.0 is accelerating the upgrading of the PC industry
- swiper. JS to achieve barrage effect
- 复现Thinkphp 2.x 任意代码执行漏洞
- Appium automation test foundation - appium basic operation API (II)
- Appium automation test foundation - appium basic operation API (I)
- 机械臂速成小指南(九):正运动学分析
- Li Kou today's question -729 My schedule I
- F. Min cost string problem solving Report
- MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
猜你喜欢
Data communication foundation OSPF Foundation
Six common transaction solutions, you sing, I come on stage (no best, only better)
我们为什么要学习数学建模?
Intelligent metal detector based on openharmony
Nine hours, nine people, nine doors problem solving Report
Cs231n notes (top) - applicable to 0 Foundation
【网易云信】超分辨率技术在实时音视频领域的研究与实践
Data communication foundation - Ethernet port mirroring and link aggregation
21. [STM32] I don't understand the I2C protocol. Dig deep into the sequence diagram to help you write the underlying driver
定义严苛标准,英特尔Evo 3.0正在加速PC产业升级
随机推荐
Example project: simple hexapod Walker
Background system sending verification code function
开发中Boolean类型使用遇到的坑
Temporary cramming before DFS examination
Lesson 4 knowledge summary
机械臂速成小指南(九):正运动学分析
对象和类的关系
wxml2canvas
Use of RLOCK lock
Clock switching with multiple relationship
基于OpenHarmony的智能金属探测器
Verilog realizes the calculation of the maximum common divisor and the minimum common multiple
list使用Stream流进行根据元素某属性数量相加
episodic和batch的定义
Analytic hierarchy process of mathematical modeling (including Matlab code)
Modify PyUnit_ Time makes it support the time text of 'xx~xx months'
OceanBase社区版之OBD方式部署方式本地安装
六种常用事务解决方案,你方唱罢,我登场(没有最好只有更好)
The OBD deployment mode of oceanbase Community Edition is installed locally
wyt 。。