jdk1.8出了很多新特性,对集合操作很多,整理了一下方法,供大家使用
Map<String,Supplier> map = materialSupplierList.stream().collect(Collectors.toMap(MaterialSupplier::getMaterialCode, supplier -> supplier));
List<Long> aggregationItemIdList= resultList.stream().map(PlanAggregationItemListVO::getId).collect(Collectors.toList());
Set<String> materialCodeSet = planItemList.stream().map(PlanItemVO::getMaterialCode).collect(Collectors.toSet());
String stationCodes = dataList.stream().map(Station::getStationCode).collect(Collectors.joining(","));
分享一个拼接字符串的方法
// filter过滤组成新的集合
List<StockTransferOrderStatus> statusList = new ArrayList<>();
List<StockTransferOrderStatus> suspended = statusList.stream().filter(status -> status == StockTransferOrderStatus.SUSPENDED).collect(Collectors.toList());
List<UsagePrice> prices = pricelist.stream().filter(usagePrice -> {
if (periodBeginTime.isAfter(usagePrice.getEffectBeginTime()) &&
periodBeginTime.isBefore(usagePrice.getEffectEndTime())) {
return true;
}
return false;
}).collect(Collectors.toList());
//分组
Map<Long, List<ShippingBatchItem>> groupMap = batchItemListFiler.stream().collect(Collectors.groupingBy(ShippingBatchItem::getShippingBatchId));
//数量累加求和场景1 -int 求和
Long shouldCountSum = asnItems.stream().map(OutBoundShippingNoticeItemPo::getCurrQuantity).reduce(Long::sum).get();
//数量累加求和场景2 -BigDecimal求和
BigDecimal result2 = userList.stream()
// 将user对象的age取出来map为Bigdecimal
.map(User::getAge)
// 使用reduce()聚合函数,实现累加器
.reduce(BigDecimal.ZERO,BigDecimal::add);
List<ReceiptRecordPo> receiptRecordPos = new ArrayList<>();
goodsReciptEventList.stream().forEach(item -> receiptRecordPos.add(ReceiptRecordPo.build(userName, item, objectMapper)));
//MAP
Map<Long, ReceiptItemPo> receiptItemPoMap = receiptItemList.stream().collect(Collectors.toMap(ReceiptItemPo::getId, item -> item));
Map<Long,UnifiedInventoryEs> map = esList.stream().collect(Collectors.toMap(UnifiedInventoryEs::getUnifiedInventoryId, Function.identity()));
//集合快速过滤设值
list.stream().forEach(item-> {
item.setEnabledName(item.getEnabled() ? "启用":"禁用");
});
//按照集合里的参数 拼接成目标集合
List<String> improtSnList = list.stream().map(d ->d.getMaterialCode()+"_"+d.getMaterialSn()).collect(Collectors.toList());
//集合生成另外一个集合
List<BillingItemDailyPrivate> dailyPrivateList = usagePrivateList.stream().map(usagePrivate -> {
BillingItemDailyPrivate dailyPrivate = new BillingItemDailyPrivate();
BeanUtils.copyProperties(usagePrivate, dailyPrivate);
return dailyPrivate;
}).collect(Collectors.toList());
//通过条件过滤其中数据,再拼装成集合
List<RmaReturnDetailSn> changePnList = rmaReturnDetailSnList.stream().filter(iteam ->NoRmaReasonDetailTypeEnum.MIX.getType().equals(iteam.getNoRmaReasonDetailType()) && !iteam.getMaterialCode().equals(iteam.getMixMaterialCode())).collect(Collectors.toList());
//将集合转成另外一种对象集合
List<UpdateMaterialCodeBySn> repairOrderSnTempList = changePnList.stream().map(RmaReturnDetailSn::buildUpdateMaterialCodeBySn).collect(Collectors.toList());
//List对象转换成map对象
private Map<Long, List<CloudEntryProjectBindingDto>> getRmProjectIdAndBindingMap(List<CloudEntryProjectBindingDto> rmProjectBindingDtoList) {
return rmProjectBindingDtoList ==null?null:rmProjectBindingDtoList.stream().collect(Collectors.toMap(CloudEntryProjectBindingDto::getProjectId, s -> {
List<CloudEntryProjectBindingDto> l = new ArrayList<>();
l.add(s);
return l;
}, (List<CloudEntryProjectBindingDto> s1, List<CloudEntryProjectBindingDto> s2) -> {
s1.addAll(s2);
return s1;
}));
}
//集合中按照某个字段值正序排列集合对象
List<BillingRealCostDto> sorted = realCostDtos.stream().sorted(Comparator.comparingInt(BillingRealCostDto::getMonth)).collect(toList());
//按照id集合过滤对象集合返回新的集合
returnList = proxyDtos.stream().filter(proxyDto->{
for(Long id:idList){
if(proxyDto.getId().equals(id)){
return true;
}
}
return false;
}).collect(Collectors.toList());