I have a list of Integer
list
and from the list.stream()
I want the maximum value. What is the simplest way? Do I need comparator?
You can also use below code snipped:
int max = list.stream().max(Comparator.comparing(Integer::valueOf)).get();
Another alternative:
list.sort(Comparator.reverseOrder()); // max value will come first
int max = list.get(0);
Correct code:
int max = list.stream().reduce(Integer.MIN_VALUE, (a, b) -> Integer.max(a, b));
or
int max = list.stream().reduce(Integer.MIN_VALUE, Integer::max);