Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) 
like image 435
flo Avatar asked Feb 13 '15 12:02

flo


People also ask

How do you handle null in comparator?

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.

Why use Optional instead of null check?

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.


1 Answers

Found a possible solution:

Comparator.comparing(ToSort::getSortBy,        Comparator.nullsFirst(Comparator.naturalOrder()) ) 
like image 104
flo Avatar answered Sep 20 '22 20:09

flo