I have the following snippet of code, where items is a list of type List<BigDecimal>:
BigDecimal output = items.stream()
.filter(Objects::notnull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
And I have the following test cases:
a. Input -> List<BigDecimal> items = Arrays.asList(null, 4);
Expected output: 4
b. Input -> List<BigDecimal> items = Arrays.asList(null, 4, 3);
Expected output: 7
c. Input -> List<BigDecimal> items = Arrays.asList(null, null);
Expected output: null
My code is failing for test-case C, the output it produces is a BigDecimal having the value of 0 instead of null.
How can I fix that?