jdk 1.8 stream

1.将集合中某字段抽取去重为集合

List<String> pillarsSidList = sdPillarsInstalls.stream().map(SdPillarsInstall::getPillarsSid).distinct().collect(Collectors.toList());

2.将集合中某字段提取为MAP

Map<Long, NyNybtPurchaseInfoReport> purchaseOrderIdMap = purchaseOrderList.stream().collect(Collectors.toMap(NyNybtPurchaseInfoReport::getId, Function.identity()));

3.将集合中某字段提取为集合

List<String> detailIdList = childrenList.stream().map(NyNybtSubsidyGatherReportDetail::getDetailIds).collect(Collectors.toList());

4.将集合中某字段提取进行拼接成字符串

String orderNumberStr = purchaseOrderRepeatList.stream().map(NyNybtPurchaseInfoReport::getOrderNumber).collect(Collectors.joining(","));

5.将集合中某字段提取为MAP<String,List>

Map<String, List<NyNybtPurchaseInfoReport>> map = purchaseOrderList.stream().collect(Collectors.groupingBy(NyNybtPurchaseInfoReport::getPurchaserSid));

 6.外部定义集合 循环将集合中元素的某字段提取到外部定义集合中

  List<String> purchaseOrderIdList = new ArrayList<>();
        List<NyNybtSubsidyGatherReportDetail> detailList=new ArrayList<>();
        NyNybtSubsidyGatherReportDetail detail1 = new NyNybtSubsidyGatherReportDetail();
        detail1.setDetailIds("1,2,3");
        detailList.add(detail1);
        //这样purchaseOrderIdList  是收集不到元素的
        detailList.stream().map(a -> purchaseOrderIdList.addAll(Arrays.asList(Convert.toStrArray(a.getDetailIds()))));
        //加上.collect(Collectors.toList());才可以终止流 获得结果
        detailList.stream().map(a -> purchaseOrderIdList.addAll(Arrays.asList(Convert.toStrArray(a.getDetailIds())))).collect(Collectors.toList());
        for (String s : purchaseOrderIdList) {
            System.out.println(s);
        }
//或者
List<String> purchaseOrderIdList = new ArrayList<>();
List<NyNybtSubsidyGatherReportDetail> detailList=new ArrayList<>();
NyNybtSubsidyGatherReportDetail detail1 = new NyNybtSubsidyGatherReportDetail();
detail1.setDetailIds("1,2,3");
detailList.add(detail1);
detailList.stream().forEach(a -> purchaseOrderIdList.addAll(Arrays.asList(Convert.toStrArray(a.getDetailIds()))));
 

7.stream 求和

 List<NyNybtSubsidyGatherReportDetail> detailList=new ArrayList<>();
        NyNybtSubsidyGatherReportDetail detail1 = new NyNybtSubsidyGatherReportDetail();
        detail1.setCanApplyBalance(new BigDecimal("15"));
        NyNybtSubsidyGatherReportDetail detail12 = new NyNybtSubsidyGatherReportDetail();
        detailList.add(detail1);
        detailList.add(detail12);
        BigDecimal reduce = detailList.stream().filter(b->b.getCanApplyBalance()!=null).map(a -> a.getCanApplyBalance()).reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println(reduce);

 

posted @ 2024-01-11 18:37  恃才傲物123  阅读(9)  评论(0编辑  收藏  举报