BigDecimal getInterest(List<Investment> investments) {
BigDecimal interest = BigDecimal.ZERO;
for (Investment i: investments) {
i.getTransactions().stream()
.map(Transaction::getAmount)
.forEach(interest::add);
}
return interest;
}
The problem with this method is that it always returns zero. It looks like .forEach()
is not consuming it's argument. However if I write it the way below, everything is working fine. Anyone got idea why the first method is not working?
BigDecimal getInterest(List<Investment> investments) {
BigDecimal interest = BigDecimal.ZERO;
for (Investment i: investments) {
interestPaid = interest.add(i.getTransactions().stream()
.map(Transaction::getAmount)
.reduce(BigDecimal.ZERO, BigDecimal::add));
}
return interest;
}