本文主要是介绍Java8 stream 统合应用: 取其中字段/分组统计/排序/过滤查找等,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
用java8的lambda函数式确实简化了很久代码 , 备忘一下:
@Overridepublic GetSNMasterDataResponse getSNMasterRecordsByShipPlan(ShipPlan pvShipPlan) {//get sn master table from d365 serviceD365DownloadSerialResponse lvD365DownloadSerialResponse=kcd365ClientService.downloadSerial();if (lvD365DownloadSerialResponse.errCode!=0){throw new RuntimeException(lvD365DownloadSerialResponse.errMsg);}//collect partno# listList<String> lvPartList= pvShipPlan.shipPlanDetail.stream().map(i->i.partNo).collect(Collectors.toList());GetSNMasterDataResponse lvRet=new GetSNMasterDataResponse();lvRet.result=new GetSNMasterDataResponse.ResultBean();//return SN Master records with filter nexstgoPart in partno# listlvRet.result.setSnMaster(lvD365DownloadSerialResponse.result.stream().filter(rec->{return lvPartList.contains(rec.nexstgoPart);}).collect(Collectors.toList()));final List<GetSNMasterDataResponse.ShipPlanPartInfo> lvShipPlanParts=new ArrayList<>();pvShipPlan.shipPlanDetail.stream().forEach(o->{GetSNMasterDataResponse.ShipPlanPartInfo lvRec=new GetSNMasterDataResponse.ShipPlanPartInfo();lvRec.partNo=o.partNo;lvRec.reqQty=o.reqQty;//create subset for current partnoList<SNMaster> lvSNListByPart=lvRet.result.getSnMaster().stream().filter(i->i.nexstgoPart.equals(lvRec.partNo)).collect(Collectors.toList());lvRec.setOHQty(lvSNListByPart.size());//count by MFGDate & bin#Map<String,Long> lvCountMap= lvSNListByPart.stream().collect(Collectors.groupingBy(n->n.getMFGDate()+","+n.getBin(),Collectors.counting()));//convert map to listfinal List<GetSNMasterDataResponse.BinBean> lvBeans=lvCountMap.entrySet().stream().sorted(Comparator.comparing(e->e.getKey())).map(e->new GetSNMasterDataResponse.BinBean(e.getKey().split(",")[1],e.getKey().split(",")[0],e.getValue()+"KT")).collect(Collectors.toList());// update snPrefixlvBeans.stream().forEach(n->{//find first record by Bin# & MFGDaten.setSnPrefixs(StringUtils.left(lvSNListByPart.stream().filter(sn->sn.getBin().equals(n.getBinNo())&& sn.mFGDate.equals(n.getMfgDate())).findFirst().orElse(new SNMaster("")).getSn(),6));});lvRec.setBinBeans(lvBeans);lvShipPlanParts.add(lvRec);});// sort the list by partno and returnlvRet.result.setShipPlanPartInfos(lvShipPlanParts.stream().sorted(Comparator.comparing(i->i.getPartNo())).collect(Collectors.toList()));return lvRet;}
分组统计的一简单例子:
~~~
Map<Integer,DoubleSummaryStatistics> bakdMaps=orgMates.stream().collect(Collectors.groupingBy(DsMoMate::getItemid,Collectors.summarizingDouble(i->i.getBakqty()==null?0:i.getBakqty().doubleValue())));
~~~
这篇关于Java8 stream 统合应用: 取其中字段/分组统计/排序/过滤查找等的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!