如何使用 java 8 将总体总值与 Stream 中的每个记录值进行比较

问题描述 投票:0回答:1

我有 BalancedetailList,其中我需要迭代和累积 Orginalvale,并将其与该列表中的每个债务值进行多次白化迭代进行比较。

    BigDecimal cumulatetotamt= new BigDecimal(0);
    BigDecimal thresholdRem= new BigDecimal(0);

第一次迭代

         for (Balance bal : BalanceList) {
                for (Adjustments adj: AdjustmentsReportList) {
                        if (adj.getminval().equals(bal.thresholdval())) {
                        cumulatetotamt= cumulatetotamt.add(bal.orginalval());
                                    }
                    }
            }
    

    Second iteration

    for (Balance bal : BalanceList) {
          for (Adjustments adj: AdjustmentsReportList) {
               thresholdRem= adj.val1().subtract(adj.val2())
                if (cumulatetotamt.compareTo(thresholdRem) > 0){
                       
                 }
           }
      }

如何使用 Stream l 使其成为单次迭代

java loops stream java-stream
1个回答
0
投票

第一次改成这样可行吗

BigDecimal totalBillAmt = BalanceList.stream()
                .flatMap(bal -> AdjustmentsReportList.stream()
                        .filter(adj -> adj.getBillCode().equals(bal.getSellQuotaNo()))
                        .map(Bill::getBillAmt))
                .reduce(BigDecimal.ZERO, BigDecimal::add);

第二次使用映射而不是循环?

© www.soinside.com 2019 - 2024. All rights reserved.