我正在计算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);
}
}
答案 0 :(得分:2)
您也许可以将前两个语句合并为一个:
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
。