Table of Contents
This article discusses how to sum BigDecimal
using stream. One of the significant features in Java 8 is the introduction of stream functionality and a way of carrying out the sum operation on streams of numbers like BigDecimal
.
BigDecimal
objects are immutable like String. Once immutable objects are created they can not be modified. So when it comes to performing arithmetic operations such as addition, multiplication, etc they are not friendly.
Sum BigDecimal in Java 5/6/7
Let’s recap, what summation or addition worked like before Java 8.
- First, initialize the
bigDecimalsList
variable of data typeBigDecimal
List. - Add some
BigDecimal
values to it. - Create an object
addBigDecimals
to store the total sum ofBigDecimal
values. - Iterate over Array
bigDecimalsList
using for loop.- Invoke the
add()
method. - Pass a new
BigDecimal
object obtained from integer value form theBigDecimal
from list.
- Invoke the
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class BigDecimalSum{ public static void main(String []args){ List<BigDecimal> bigDecimalsList = new ArrayList<>(); bigDecimalsList.add(new BigDecimal(1)); bigDecimalsList.add(new BigDecimal(2)); bigDecimalsList.add(new BigDecimal(3)); bigDecimalsList.add(new BigDecimal(4)); bigDecimalsList.add(new BigDecimal(5)); BigDecimal addBigDecimals = new BigDecimal(0); for (BigDecimal value : bigDecimalsList) { addBigDecimals = addBigDecimals .add(new BigDecimal(value.intValue())); } System.out.println(addBigDecimals); } } |
Output:
Sum BigDecimal in Java 8
Java 8 introduces the reduce()
method that covers the terminal operations such as sum, min, max, average, and count, etc individually in their own way.
You can also pass Lambda Expression in reduce()
method. The identity and the accumulator is passed in reduce() method to sum an ArrayList
.
The definition of the reduce()
method is given below.
1 2 3 4 |
T reduce(T identity, BinaryOperator<T> accumulator) |
You can use the stream with List, Array and Map. Let us see each of them one by one.
BigDecimal sum using List
You can store the BigDecimal
in a list by adding each BigDecimal
as a new list element. To sum the big decimal in a list,
- Initialize the List of
BigDecimal
. - Instead of iterating over for loop, you can use stream().reduce() method and save the sum in a variable.
- The reduce() method accepts
BigDecimal.ZERO
as the identity and theBigDecimal::add
as the accumulator. - The method returns the sum.
You can also sum using the lambda expression by following steps given below.
- Pass the lambda expression to the
reduce()
method that adds two variables. - The
reduce()
method returns the resultant sum.
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class BigDecimalSum{ public static void main(String []args){ List<BigDecimal> bigDecimalsList = new ArrayList<>(); bigDecimalsList.add(new BigDecimal(1)); bigDecimalsList.add(new BigDecimal(2)); bigDecimalsList.add(new BigDecimal(3)); bigDecimalsList.add(new BigDecimal(4)); bigDecimalsList.add(new BigDecimal(5)); BigDecimal addBigDecimals = bigDecimalsList.stream().reduce( BigDecimal.ZERO, BigDecimal::add); System.out.println(addBigDecimals); //Using Lambda Expression BigDecimal addBigDecimalsLE = bigDecimalsList.stream().reduce(BigDecimal.ZERO, (p, q) -> p.add(q)); System.out.println(addBigDecimalsLE); } } |
Output:
15
BigDecimal Sum Using Array
If the BigDecimal
numbers are stored in an array, you can follow the given steps to get the sum.
- Pass the array to the
stream()
method of the Arrays class. - Invoke the
reduce()
method by passing theBigDecimal.ZERO
, andBigDecimal::add
to it. - The method returns the sum, store it, and print it.
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.math.BigDecimal; import java.util.Arrays; public class BigDecimalSum{ public static void main(String []args){ BigDecimal[] bigDecimalsArray = {new BigDecimal("1.6"), new BigDecimal("2"), new BigDecimal("3.2"), new BigDecimal("4")}; BigDecimal addBigDecimals = Arrays.stream(bigDecimalsArray).reduce(BigDecimal.ZERO, BigDecimal::add); System.out.println(addBigDecimals); } } |
Output:
BigDecimal Sum Using Map
If you have stored the BigDecimal
numbers in a map, you can follow the given steps to obtain the sum.
- Invoke the
values()
method of the map. - Invoke the
stream().reduce()
method in chain with the values() method. - Pass the
BigDecimal.ZERO
andBigDecimal::add
to thereduce()
method. - The method returns sum of all
BigDecimal
values.
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; public class BigDecimalSum{ public static void main(String []args){ Map<Integer, BigDecimal> bigDecimalMap = new HashMap<>(); bigDecimalMap.put(1, new BigDecimal("1.6")); bigDecimalMap.put(2, new BigDecimal("2")); bigDecimalMap.put(3, new BigDecimal("3.2")); bigDecimalMap.put(4, new BigDecimal("4.4")); BigDecimal addBigDecimals = bigDecimalMap.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add); System.out.println(addBigDecimals); } } |
Output:
Stream Removes Null Values
Sometimes BigDecimal
List might contain a NULL
value which will raise the NullPointerException. You can resolve this issue with the help of stream.
Let us see an example where a NULL
value results in an exception.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class BigDecimalSum{ public static void main(String []args){ List<BigDecimal> bigDecimalsList = new ArrayList<>(); bigDecimalsList.add(new BigDecimal(1)); bigDecimalsList.add(new BigDecimal(2)); bigDecimalsList.add(new BigDecimal(3)); bigDecimalsList.add(new BigDecimal(4)); bigDecimalsList.add(null); BigDecimal addBigDecimals = bigDecimalsList.stream() .reduce(BigDecimal.ZERO, BigDecimal::add); System.out.println(addBigDecimals); } } |
Output:
Using a stream you can Filter NULL
values to avoid this type of exception. The filter()
method takes an argument and filters the elements according to it.
You can pass the Objects::nonNull
filter to the filter()
method.
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Objects; public class BigDecimalSum{ public static void main(String []args){ List<BigDecimal> bigDecimalsList = new ArrayList<>(); bigDecimalsList.add(new BigDecimal(1)); bigDecimalsList.add(new BigDecimal(2)); bigDecimalsList.add(new BigDecimal(3)); bigDecimalsList.add(new BigDecimal(4)); bigDecimalsList.add(null); BigDecimal addBigDecimals = bigDecimalsList.stream() .filter(Objects::nonNull) .reduce(BigDecimal.ZERO, BigDecimal::add); System.out.println(addBigDecimals); } } |
Output:
Conclusion
In this article, you learned about how Java prior to version 8 can find the sum of the BigDecimal
numbers. Then we discovered how java 8 uses stream to find sum of the BigDecimal
numbers.
The stream can be used with List, Array & Map to find the summation. We can filter null values using stream. However, the stream can be applied with other data types as well.
This was all about how to sum BigDecimal
using Stream in Java
Hope you have enjoyed reading the article. Stay tuned for more such articles.
Happy Learning!