jdk1.8集合操作新特性

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());
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值