The following code return Nullpointer exception:
Comparator<LocalDate> c = (LocalDate d1,LocalDate d2)-> {return (d1.isAfter(d2) ? 1 : (d1.isBefore(d2)? -1: 0));} ;
List<LocalDate> list1 = new ArrayList<LocalDate>();
list1.add(null);
list1.add(null);
Optional<LocalDate> lastUsedDate = list1.stream()
.max(Comparator.nullsLast(c));
System.out.println(lastUsedDate.get());
Can someone please help me understand how this can be resolved ?
As said, Stream#max does not allow
nullvalue as a result (so does notStream#min).The Stream API is not null-friendly, so neither
reducenorfindFirst/findAnycooperate if the resulting element isnull(not missing! but reallynull).You can do this: