Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum BigDecimal values in List with lambda [duplicate]

I have a simple class:

class Simple {
    private String count;
    private BigDecimal amount;
    private String label;
}

and have a List: List<Simple> simples = new ArrayList<>(); how I can sum all amounts of all simples in list with Lambda in Java 8?

like image 434
Jack Daniel Avatar asked Jun 30 '16 13:06

Jack Daniel


1 Answers

It is quite easy with a Stream and a reducer :

BigDecimal sum = simples
    .stream()
    .map(Simple::getAmount)
    .reduce(BigDecimal::add)
    .get();
like image 158
Arnaud Denoyelle Avatar answered Oct 23 '22 19:10

Arnaud Denoyelle