How to Sum BigDecimal Values with Java Stream API and Reduce

This tutorial explains how to use Java's Stream API to sum numeric collections, demonstrates summing IntStream and DoubleStream values, and shows how to apply the reduce method to calculate sums for BigDecimal streams when a dedicated BigDecimalStream is unavailable.

Programmer DD
Programmer DD
Programmer DD
How to Sum BigDecimal Values with Java Stream API and Reduce

1. Introduction

We usually use Java Stream API to process collections. A useful feature is the sum operation for numeric streams, but it cannot handle all numeric types.

This article shows how to perform sum operations on streams of types such as BigDecimal.

2. How to Sum with Streams

Stream API provides numeric streams: IntStream, DoubleStream, and LongStream. Example using IntStream#sum:

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

Similarly, we can sum a list of Doubles by converting the object stream to a DoubleStream with mapToDouble :

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

We would like the same approach for a collection of BigDecimal values.

3. Using Reduce for BigDecimal

We use Stream.reduce to compute the sum:

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

This works for any data that can be logically added, including 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:

Identity – the initial value, e.g., 0, which serves as the starting point of the reduction.

Accumulator function – a function that accepts the current accumulated result and the next element from the stream.

4. Conclusion

In this article we examined how to calculate the sum of numbers in numeric streams and how to use reduce as an alternative, especially for types like BigDecimal that lack a dedicated stream implementation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBigDecimalStream APIreduceSum
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.