Java 8 introduces a new default method on the List
interface to sort it. It's signature is :
void sort(Comparator<? super E> c)
The documentation says:
If the specified comparator is null then all elements in this list must implement the Comparable interface and the elements' natural ordering should be used.
So if you want to sort the list by it's natural order (and that your elements are comparable) you have to do list.sort(null);
which is kind of weird of my opinion.
If they used an Optional
the doc would stated that you can optionally provide a comparator, and that if it's not provided it will assume the elements are already comparable.
A list.sort(null);
call would be transformed into list.sort(Optional.empty());
.
As it's a method that it exposed to the outside world, I would find it more accurate.
Why didn't they used the new Optional API instead?