//实体类
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中的对象的某个属性,求和
int mathSumInt = list.stream().mapToInt( Student::getMathScoresInt ).sum(); //int类型
long mathSumLong = list.stream().mapToLong( Student::getMathScoresLong ).sum(); //long类型
double mathSumDouble = list.stream().mapToDouble( Student::getMathScoresDouble ).sum(); //double类型
BigDecimal mathSumBigDecimal = list.stream().map( Student::getMathScoresBigDecimal ).reduce(BigDecimal.ZERO, BigDecimal::add); //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);
BigDecimal averageBigDecimal = list.stream().map(Student::getMathScoresBigDecimal).reduce(BigDecimal.ZERO, BigDecimal::add).divide(BigDecimal.valueOf(list.size()), 2, RoundingMode.HALF_UP);
三、根据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推荐用这种
BigDecimal mathMinBigDecimal = list.stream().map( Student::getMathScoresBigDecimal ).max(BigDecimal::compareTo).get();
//全是负数时,这种比较结果,会有错误,最大值恒等于0
BigDecimal mathMaxBigDecimal2 = 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推荐用这种
BigDecimal mathMinBigDecimal = list.stream().map( Student::getMathScoresBigDecimal ).max(BigDecimal::compareTo).get();
//全是正数时,这种比较结果,会有错误,最小值恒等于0
BigDecimal mathMinBigDecimal2 = list.stream().map( Student::getMathScoresBigDecimal ).reduce(BigDecimal.ZERO, BigDecimal::min);
五、根据List中的对象的某个属性,找出最大N个对象/最小N个对象
解题思路:把list集合,按照某个属性升序/降序,再取N个对象
//将List转换List 【不过滤,不去重,升序,取有限数】 (本案例:取年龄最小2位)
List<Student> studentList =
list.stream().sorted(Comparator.comparing(Student::getAge)).limit(2).collect(Collectors.toList());
//将List转换List 【不过滤,不去重,降序,取有限数】 (本案例:取年龄最大2位)
List<Student> studentList =
list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).limit(2).collect(Collectors.toList());
【备注】详情见我另一篇文章: https://blog.csdn.net/SeniorShen/article/details/120263318