關於list.Stream的用法(去重,相加,分組)


1.通過list中的某個字段來對list分組,然后得到多組數據
   Map<String, List<WmsStockRefundItem>> refundMap = itemList.stream().collect(Collectors.groupingBy(WmsStockRefundItem::getSupplyNumber));

2.通過list中的某個屬性過濾去重,只留下不同的集合

List<WmsStockRefundItem> fiterList =list.stream().filter(distinctByKey(b -> b.getProdNumber())).collect(Collectors.toList());

//通過屬性去重
private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object,Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

3.相加list中的某個值 bigdecimal

 BigDecimal totalquantity = list.stream().map(WmsStockRefundItem::getReqQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);

 

4.相加list中的某個值 Integer

Integer num=list.stream().collect(Collectors.summingInt(WmsStockRefundItem::getReqQuantity))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM