I have following code:
List<LocalDate> dates = Arrays.asList(null, null,
LocalDate.now(), LocalDate.now().minusDays(9));
LocalDate max = dates.stream()
.max(Comparator.nullsLast(Comparator.naturalOrder())).get();
Which produce null pointer exception when I try to get the max, but when trying to get min it works ok. It seems unclear cause max
takes Comparator
which handles null values. How to sort such array with stream preserving null values.
By the way, I checked JavaDoc of max
, which states:
@throws NullPointerException if the maximum element is null
EDIT:
LocalDate max = dates.stream()
.max(Comparator.nullsLast(Comparator.naturalOrder())).orElse(null);
Throws null pointer as well.