BigDecimal 汇总统计

我有一个 BigDecimals 列表。

List<BigDecimal> amounts = new ArrayList<>()

我如何使用 Java 8 流获取上述列表的汇总统计数据,而不会丢失 BigDecimal 最多 3-4 个小数位的精度?


扬帆大鱼
浏览 231回答 3
3回答

跃然一笑

如果您愿意使用第三方库(与 Java 8 流兼容),您可以使用jOOλ,您可以使用它编写:Tuple5<&nbsp; &nbsp; Long,&nbsp;&nbsp; &nbsp; Optional<BigDecimal>,&nbsp;&nbsp; &nbsp; Optional<BigDecimal>,&nbsp;&nbsp; &nbsp; Optional<BigDecimal>,&nbsp;&nbsp; &nbsp; Optional<BigDecimal>> tupleamounts.stream()&nbsp; &nbsp; &nbsp; &nbsp;.collect(Tuple.collectors(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Agg.sum(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Agg.count(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Agg.avg(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Agg.<BigDecimal>min(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Agg.<BigDecimal>max()&nbsp; &nbsp; &nbsp; &nbsp;));这不会导致精度损失,但可能比聚合双精度慢得多
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java