Find max on BigDecimal list using java 8

Viewed 16728

Let's suppose that I have a class like this :

public class A {
    private int id;
    private BigDecimal amount;
}

And I have a List<A>. How can I find the maximum amount of all A objects in the list using Java 8 stream ?

This method :

List<A> brol = new ArrayList<>();
BigDecimal max = brol.stream().max(Comparator.comparing(A -> A.getAmount())).get().getAmount();
        System.out.println("MAX = " + max);

Gives NoSuchElementException so I should create a specific comparator for this ?

2 Answers
Related