代码举例
@Slf4j
public class Test {
public void test() throws IOException {
User user1 = new User(2, "Steven", new Date(), 2000.0, new BigDecimal(2000));
User user2 = new User(3, "Steven", new Date(), 3000.0, new BigDecimal(3000));
User user3 = new User(4, "Steven", new Date(), 4000.0, new BigDecimal(4000));
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(user2);
userList.add(user3);
BigDecimal max = userList.stream().map(User::getWeight).max((x1, x2) -> x1.compareTo(x2)).get();
BigDecimal min = userList.stream().map(User::getWeight).min((x1, x2) -> x1.compareTo(x2)).get();
BigDecimal sum = userList.stream().map(vo -> ObjectUtils.isEmpty(vo.getWeight()) ? new BigDecimal(0) : vo.getWeight()).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal average = userList.stream().map(vo -> ObjectUtils.isEmpty(vo.getWeight()) ? new BigDecimal(0) : vo.getWeight()).reduce(BigDecimal.ZERO, BigDecimal::add).divide(BigDecimal.valueOf(userList.size()), 2, BigDecimal.ROUND_HALF_UP);
log.info(max.toString());
log.info(min.toString());
log.info(sum.toString());
log.info(average.toString());
}
}