首页 > 解决方案 > 有没有更好的方法来使用流重构这段代码

问题描述

我正在计算 3 个值,它们是列表对象的总数。所以为了得到这些,我写了 3 个语句,每个语句用于计算 3 个值。我想知道有什么办法可以用一个流(功能块)之类的东西做得更好

尝试使用 3 个语句来计算来自相同列表或流的每个总数

BigDecimal totalMkt = subAccounts.parallelStream()
        .flatMap(ts -> ts.getAssets().parallelStream())
        .filter(ast -> !ast.getAssetTypeCode().equals(AssetType.CURRENCY))
         .map(ast -> ast.getPostMktVal())
        .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);

BigDecimal totalTradeVal = subAccounts.parallelStream()
        .flatMap(ts -> ts.getAssets().parallelStream())
        .filter(ast -> !ast.getAssetTypeCode().equals(AssetType.CURRENCY))
        .map(ast -> ast.getTradeVal())
        .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);

BigDecimal totalValue = totalMkt.add(totalTradeVal).add(totalWrk);

logger.debug("totalMkt "+totalMkt+ 
            " totalTradeVal "+totalTradeVal +
            " totalWrk "+totalWrk +
            " totalValue "+totalValue);

subAccounts.stream()
        .flatMap(subAccount -> subAccount.getAssets().stream()
            .filter(asset->!asset.getAssetTypeCode().equals(AssetType.CURRENCY)))
        .forEach(asset -> {
            logger.debug("assetCode "+asset.getAssetCode());
            BigDecimal weightPct = asset.getPostMktVal()
                .multiply(new BigDecimal(100))
                .divide(totalValue, 5,RoundingMode.HALF_UP);
            asset.setWeightPct(weightPct);
            logger.debug(" weightPct " + weightPct);
        });

@Tarlog 建议的新创建的 Pair 类。当多个请求进来时,这是否有任何影响或问题

    static class Pair<L,R> {
          final L left;
          final R right;

          public L getLeft() {
            return left;
        }

        public R getRight() {
            return right;
        }

        public Pair(L left, R right) {
            this.left = left;
            this.right = right;
          }

          static <L,R> Pair<L,R> of(L left, R right){
              return new Pair<L,R>(left, right);
          }
    }

标签: javajava-stream

解决方案


您也许可以将前两个语句合并为一个:

Pair<BigDecimal, BigDecimal> totalMktTrade = subAccounts.parallelStream()
        .flatMap(ts -> ts.getAssets().parallelStream())
        .filter(ast -> !ast.getAssetTypeCode().equals(AssetType.CURRENCY))
         .map(ast -> Pair.of(ast.getPostMktVal(), ast.getTradeVal()))
        .reduce((a,b) -> Pair.of(a.getLeft().add(b.getLeft(), a.getRight().add(b.getRight());
BigDecimal totalMkt = totalMktTrade.getLeft();
BigDecimal totalTradeVal = totalMktTrade.getRight();

我看不出如何组合第三个流,因为它使用先前计算的结果,包括totalWrk来自外部的计算结果。


推荐阅读