124

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)

2 Answers 2

225
+200

Found a possible solution:

Comparator.comparing(ToSort::getSortBy, 
      Comparator.nullsFirst(Comparator.naturalOrder())
)
Sign up to request clarification or add additional context in comments.

1 Comment

So the reason this solution works is because of the order of nesting of comparators. In your question the innermost comparator (and therefore the first to be executed) was ToSort::getSortBy and the nullsFirst was wrapped around it. In this answer the nullsFirst comparator is now executed first, providing safety before the null-returning getSortBy calls
7

Snippet for streams:

somePersonList
    .stream()
    .sorted(
        Comparator.comparing(SomePerson::getName,
        Comparator.nullsLast(String::compareTo))
    )
    .collect(Collectors.toList())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.