null-safe mapping Comparator using default implementations
Is there a build-in possibility to create a null-safe mapping comparator in Java 8 without writing a own implementation of Comparator
?
When running the following code, it causes a NPE because the keyExtractor
argument of Comparator.comparing()
may return a null
value:
public class ToSort
{
private String sortBy;
public ToSort(String sortBy)
{
this.sortBy = sortBy;
}
public String getSortBy()
{
return sortBy;
}
public static void main(String[] args)
{
// mapping comparator
Comparator<ToSort> comp = Comparator.comparing(ToSort::getSortBy);
SortedSet<ToSort> set = new TreeSet<>(comp);
ToSort o1 = new ToSort("1");
ToSort o2 = new ToSort(null);
set.add(o1);
System.out.println(set.contains(o2)); //NPE because o2.getSortBy() == null
}
}
Exception in thread "main" java.lang.NullPointerException at java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469) at java.util.Comparator$$Lambda$2/1480010240.compare(Unknown Source) at java.util.Comparators$NullComparator.compare(Comparators.java:83) at java.util.TreeMap.getEntryUsingComparator(TreeMap.java:376) at java.util.TreeMap.getEntry(TreeMap.java:345) at java.util.TreeMap.containsKey(TreeMap.java:232) at java.util.TreeSet.contains(TreeSet.java:234) at test.ToSort.main(ToSort.java:48)
Using
Comparator<ToSort> comp = Comparator.nullsFirst(Comparator.comparing(ToSort::getSortBy));
does not work either as only ToSort
objects that are null
are treaded properly.
I know how to write my own Comparator
implementation, I`m just searching a more "elegant" solution like
Comparator.comparingNullsFirst(ToSort::getSortBy)
Found a possible solution:
Comparator.comparing(ToSort::getSortBy,
Comparator.nullsFirst(Comparator.naturalOrder())
)