I am trying to add all the prices of given products as:
BigDecimal getTotalPrice(List<Product> products) {
return products.stream()
.map(Product::getPrice)
.peek(bigDecimal -> System.out.println(bigDecimal))
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
I tried printing all the values of bigDecimal
, whenever we get value as null
we are getting java.lang.NullPointerException
, is there any better way to addition in the java 8
by preventing any kind of Exceptions?