If we have a list of objects, and we want to sum a BigDecimal property of these objects, we can achieve this using the Java Stream API. This API provides a clean and efficient way to process collections of objects. To sum the BigDecimal amounts, you can use the map and reduce methods of the Stream API.
As an example, we have a class named Transaction with a BigDecimal property named amount. We have a list of Transaction objects, and we want to calculate the total sum of the amount properties.
In the code snippet below we do the following:
- Creating Transactions: We create a list of
Transactionobjects, each with a differentBigDecimalamount. - Filter Transactions and its amount: We filter to exclude the
nulltransaction andnulltransaction amount. - Mapping to Amounts: We use the
mapmethod to convert eachTransactionobject to itsamountproperty. - Summing the Amounts: The
reducemethod takes two parameters: an identity value (BigDecimal.ZERO) and an accumulator function (BigDecimal::add). The accumulator function adds eachBigDecimalin the stream to the running total. - Printing the Result: Finally, we print the total sum of the amounts.
package org.kodejava.stream;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
public class BigDecimalSumExample {
public static void main(String[] args) {
// Create a list of transaction objects
List<Transaction> transactions = Arrays.asList(
new Transaction(new BigDecimal("10.50")),
null,
new Transaction(new BigDecimal("30.25")),
new Transaction(null),
new Transaction(new BigDecimal("11.49"))
);
// Sum the amount properties using stream
BigDecimal totalAmount = transactions.stream()
// Filter out null Transaction objects and Transaction objects
// with null amounts
.filter(t -> t != null && t.getAmount() != null)
.map(Transaction::getAmount)
.reduce(BigDecimal.ZERO, BigDecimal::add);
// Print the result
System.out.println("Total Amount: " + totalAmount);
}
static class Transaction {
private final BigDecimal amount;
public Transaction(BigDecimal amount) {
this.amount = amount;
}
public BigDecimal getAmount() {
return amount;
}
}
}
Below is another example, we want to sum just a List<BigDecimal> values. To sum the values we can use the reduce method as shown in the code snippet below.
package org.kodejava.stream;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class BigDecimalListSumExample {
public static void main(String[] args) {
// Create a list of BigDecimal values
List<BigDecimal> amounts = Arrays.asList(
new BigDecimal("10.50"),
new BigDecimal("20.75"),
new BigDecimal("30.25"),
null,
new BigDecimal("11.49")
);
// Sum the BigDecimal values using stream
BigDecimal totalAmount = amounts.stream()
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
// Print the result
System.out.println("Total Amount: " + totalAmount);
}
}
Using Java Stream API to sum a BigDecimal property of a list of objects or a list of BigDecimal values are both concise and efficient. The map and reduce methods streamline the process, making our code more readable and maintainable. This approach can be applied to various scenarios where we need to aggregate data from a list of objects.
