You should use Comparator.nullsLast twice:
list.sort(nullsLast(comparing(Bean::getVal, nullsLast(naturalOrder()))));
- First
nullsLastwill handle the cases when theBeanobjects are null. - Second
nullsLastwill handle the cases when the return value ofBean::getValis null.
In case you’re sure there aren’t any null values in your list then you can omit the first nullsLast (as noted by @Holger) :
list.sort(comparing(Bean::getVal, nullsLast(naturalOrder())));