Add BigDecimals using the Stream API – 使用Stream API添加BigDecimals

最后修改: 2020年 4月 4日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

We usually use the Java Stream API for processing collections of data.

我们通常使用Java Stream API来处理数据的集合。

One nice feature is support for operations on numeric streams, like the sum operation. However, we cannot process all numeric types in this way.

一个很好的功能是支持对数字流的操作,如sum操作。然而,我们不能以这种方式处理所有的数字类型。

In this tutorial, we’ll see how to perform the sum operation on streams of numbers like BigDecimal.

在本教程中,我们将看到如何对像BigDecimal这样的数字流执行sum操作。

2. How We Usually Sum a Stream

2.我们通常是如何对一个流进行总结的

The Stream API provides streams of numbers, including IntStream, DoubleStream, and LongStream.

Stream API提供了数字流,包括IntStream, DoubleStream, LongStream。

Let’s remind ourselves how these work by creating a numeric stream. Then, we’ll calculate its total with IntStream#sum:

让我们通过c创建一个数字流来提醒自己这些工作。然后,我们将用IntStream#sum来计算其总数。

IntStream intNumbers = IntStream.range(0, 3);
assertEquals(3, intNumbers.sum());

We can do a similar thing starting with a list of Doubles. By using streams, we can convert from an object stream to a DoubleStream using mapToDouble:

我们可以从一个Doubles的列表开始做类似的事情。通过使用流,我们可以使用mapToDouble从一个对象流转换为DoubleStream

List<Double> doubleNumbers = Arrays.asList(23.48, 52.26, 13.5);
double result = doubleNumbers.stream()
    .mapToDouble(Double::doubleValue)
    .sum();
assertEquals(89.24, result, .1);

So, it would be useful if we could sum up a collection of BigDecimal numbers the same way.

因此,如果我们能以同样的方式对一个BigDecimal数字的集合进行求和,那就很有用了。

Unfortunately, there isn’t a BigDecimalStream. So, we need another solution.

不幸的是,并没有一个BigDecimalStream。所以,我们需要另一个解决方案。

3. Using Reduce to Add BigDecimal Numbers

3.使用Reduce来添加BigDecimal数字

Instead of relying on sum, we can use Stream.reduce:

我们可以使用Stream.reduce来代替对sum的依赖。

Stream<Integer> intNumbers = Stream.of(5, 1, 100);
int result = intNumbers.reduce(0, Integer::sum);
assertEquals(106, result);

This works on anything that can be logically added together, including BigDecimal:

这适用于任何可以逻辑相加的东西,包括BigDecimal

Stream<BigDecimal> bigDecimalNumber = 
  Stream.of(BigDecimal.ZERO, BigDecimal.ONE, BigDecimal.TEN);
BigDecimal result = bigDecimalNumber.reduce(BigDecimal.ZERO, BigDecimal::add);
assertEquals(11, result);

The reduce method takes two parameters:

reduce方法需要两个参数。

  • Identity – is the equivalent of – it is the starting value for the reduction
  • Accumulator function – takes two parameters, the result so far, and the next element of the stream

4. Conclusion

4.总结

In this article, we looked at how to find the sum of some numbers in a numeric Stream. Then we discovered how to use reduce as an alternative.

在这篇文章中,我们研究了如何在一个数字Stream中找到一些数字的总和。然后我们发现如何使用reduce作为替代方法。

Using reduce allows us to sum a collection of BigDecimal numbers. It can be applied to any other type.

使用reduce可以让我们对BigDecimal数字的集合进行求和。它可以应用于任何其他类型。

As always, the code for the examples is available over on GitHub.

像往常一样,这些例子的代码可以在GitHub上找到over