public class ListCalculate {
public static void main(String[] args) {
List<Money> moneyList = getMoneyList();
// List中的对象属性 求和
int sumInt = ().mapToInt(Money::getAmountInt).sum();
long sumLong = ().mapToLong(Money::getAmountLong).sum();
double sumDouble = ().mapToDouble(Money::getAmountDouble).sum();
BigDecimal sumBigDecimal = ().map(Money::getAmountBigDecimal).reduce(, BigDecimal::add);
// List中的对象属性 求最大值
int maxInt = ().mapToInt(Money::getAmountInt).max().getAsInt();
long maxLong = ().mapToLong(Money::getAmountLong).max().getAsLong();
double maxDouble = ().mapToDouble(Money::getAmountDouble).max().getAsDouble();
BigDecimal maxBigDecimal = ().map(Money::getAmountBigDecimal).reduce(, BigDecimal::max);
// List中的对象属性 求最小值 注意BigDecimal
int minInt = ().mapToInt(Money::getAmountInt).min().getAsInt();
long minLong = ().mapToLong(Money::getAmountLong).min().getAsLong();
double minDouble = ().mapToDouble(Money::getAmountDouble).min().getAsDouble();
BigDecimal minBigDecimal = ().map(Money::getAmountBigDecimal).reduce(, BigDecimal::min); // 0
BigDecimal minBigDecimal2 = ().map(Money::getAmountBigDecimal).min(BigDecimal::compareTo).get(); // 143
;
// List中的对象属性 求平均值
double averageInt = ().mapToInt(Money::getAmountInt).average().getAsDouble();
double averageLong = ().mapToLong(Money::getAmountLong).average().getAsDouble();
double averageDouble = ().mapToDouble(Money::getAmountDouble).average().getAsDouble();
BigDecimal averageBigDecimal = ().map(Money::getAmountBigDecimal).reduce(, BigDecimal::add).divide((()), 2, RoundingMode.HALF_UP);
}
private static List<Money> getMoneyList() {
List<Money> moneyList = new ArrayList<>();
(new Money(111, 958L, 333.5, new BigDecimal(689)));
(new Money(723, 256L, 999.0, new BigDecimal(333)));
(new Money(135, 780L, 843.8, new BigDecimal(276)));
(new Money(564, 671L, 745.1, new BigDecimal(143)));
return moneyList;
}
}
@Data
@AllArgsConstructor
class Money {
private int amountInt;
private long amountLong;
private double amountDouble;
private BigDecimal amountBigDecimal;
}