Java8的lambda用于操作集合非常好用,这里记录一些常用API
1.从实体集合中提取某字段:字符串类型、double类型、BigDecimal类型,关键字:map、mapToDouble
//BigDecimal,提取并聚合 BigDecimal amount = orders.stream().map(JDOrder::getOrderFee).reduce(BigDecimal.ZERO,BigDecimal::add);
//String List<String> voucherIds = voucherAssigns.stream().map(v -> v.getVoucherId()).collect(Collectors.toList()); List<String> giftIds = itemGifts.stream().map( ItemGifts::getGiftId).collect(Collectors.toList()); //double,提取并聚合 Double totalAmount = soldSums.stream().filter(s -> !ObjectUtils.isEmpty(s.getSum())).mapToDouble(s -> s.getSum()).sum(); //Integer List<Integer> couponIds = itemCoupons.stream().map(i -> i.getCouponId()).collect(Collectors.toList());
2.根据某个字段过滤:可以是=、 >= ,关键字:filter
items = items.stream().filter(i -> i.getPrice()>=couponPrice).collect(Collectors.toList());