根据List中的对象的某个属性,求和

Posted 格子衫111

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据List中的对象的某个属性,求和相关的知识,希望对你有一定的参考价值。

一、根据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 kczl = entities.stream().map(BloodRkdEntity::getKcxl).reduce(BigDecimal.ZERO, BigDecimal::add);

二、根据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);

以上是关于根据List中的对象的某个属性,求和的主要内容,如果未能解决你的问题,请参考以下文章

java根据list中的对象某个属性排序

JAVA list 根据对象的某个属性排序

Java8 list根据对象某个属性去重

Java关于list集合根据集合元素对象的某个或多个属性进行排序的工具类

C# list<>根据对象的某个属性排序

List集合中的对象根据某个相同的属性,合并另外属性