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