我是靠谱客的博主 感动皮带,最近开发中收集的这篇文章主要介绍java decimal相加,使用流和求和方法的BigDecimal List的总和,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

If we have all int or long or other primitive datatype value in list then we obtain sun of all values using

return items.stream().mapToInt(i -> i).sum();

I have list of BigDecimal values,how to find the sum of all values using Java8

As there is no default method like mapToBigDecimal I tried to create plain map but then I cannot use sum()

解决方案

List items = Arrays.asList(BigDecimal.ONE, BigDecimal.valueOf(1.5), BigDecimal.valueOf(100));

items.stream().reduce(BigDecimal.ZERO, BigDecimal::add);

Though Answer provided by Lisq199 works but I am in favor of including Holger and Klitos comments,This handles No values and always returns a value

最后

以上就是感动皮带为你收集整理的java decimal相加,使用流和求和方法的BigDecimal List的总和的全部内容,希望文章能够帮你解决java decimal相加,使用流和求和方法的BigDecimal List的总和所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(98)

评论列表共有 0 条评论

立即
投稿
返回
顶部