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)
Comparator nullsLast() method in Java with examples When both elements are null, then they are considered equal. When both elements are non-null, the specified Comparator determines the order. If specified comparator is null, then the returned comparator considers all non-null elements equal.
In a nutshell, the Optional class includes methods to explicitly deal with the cases where a value is present or absent. However, the advantage compared to null references is that the Optional class forces you to think about the case when the value is not present.
Found a possible solution:
Comparator.comparing(ToSort::getSortBy, Comparator.nullsFirst(Comparator.naturalOrder()) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With