当前位置:网站首页>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);
边栏推荐
- Data communication foundation smart_ Link_&_ Monitor_ Link
- Data communication foundation NAT network address translation
- Value series solution report
- 异常com.alibaba.fastjson.JSONException: not match : - =
- MySQL表字段调整
- 20. [stm32] realize the function of intelligent garbage can by using ultrasonic module and steering gear
- Intelligent metal detector based on openharmony
- Information collection of penetration test
- The computer is busy, and the update is a little slow
- episodic和batch的定义
猜你喜欢
随机推荐
How difficult is it to pass the certification of Intel Evo 3.0? Yilian technology tells you
我们为什么要学习数学建模?
Analytic hierarchy process of mathematical modeling (including Matlab code)
Appium automation test foundation - appium basic operation API (II)
Data communication foundation ACL access control list
修改pyunit_time使得其支持‘xx~xx月’的时间文本
基于OpenHarmony的智能金属探测器
List uses stream flow to add according to the number of certain attributes of the element
Coding devsecops helps financial enterprises run out of digital acceleration
Definition of episodic and batch
20. [stm32] realize the function of intelligent garbage can by using ultrasonic module and steering gear
力扣今日题-729. 我的日程安排表 I
Replknet: it's not that large convolution is bad, but that convolution is not large enough. 31x31 convolution. Let's have a look at | CVPR 2022
Good article inventory
List de duplication and count the number
OSI seven layer model
swiper. JS to achieve barrage effect
Cs231n notes (top) - applicable to 0 Foundation
项目sql中批量update的时候参数类型设置错误
vulnhub-FirstBlood