Java 8 introduces a new default method in the List interface to sort it. His signature:
void sort(Comparator<? super E> c)
The documentation says:
If the specified comparator is zero, then all the elements in this list should implement the Comparable interface, and natural should use order.
So, if you want to sort the list in its natural order (and so that your items are comparable), you must do list.sort(null); , which is rather strange from my opinion.
If they used Optional , the document would indicate that you can optionally provide a comparator, and if it is not provided, it is assumed that the elements are already comparable.
A list.sort(null); the call will be converted to list.sort(Optional.empty()); .
As a method that he exposed to the outside world, I would find it more accurate.
Why didn't they use the new optional API instead?
source share