Collectors.minBy/maxBy behavior for null value

Tagir Valeev amaembo at
Mon Feb 22 10:06:01 UTC 2016


Consider the following code:

Comparator<String> cmp = Comparator.nullsFirst(Comparator.naturalOrder());
System.out.println(Stream.of("a", "b",

It prints Optional.empty, so the result is indistinguishable from empty
Stream (siimlar for maxBy). This behavior is not consistent with
Stream.findFirst() (which throws NPE if the resulting element is null).
Currently minBy spec says:

This produces a result equivalent to:

However reducing spec says nothing what will occur if the result of
reduction is null. To my opinion at very least the spec of reducing should
be updated to specify the behavior when reducing result is null. It
probably would also be nice to add a note into minBy/maxBy spec about this.

What do you think?

With best regards,
Tagir Valeev.

More information about the core-libs-dev mailing list