Java Comparator.nullsFirst
March 18, 2019
nullsFirst
is the static method of Comparator
functional interface. Comparator.nullsFirst
method introduced in Java 8, returns a null-friendly comparator that considers null to be less than non-null. Find its declaration from Java source code.
static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator)
nullsFirst
method.
1. The null element is considered to be less than non-null.
2. When both elements are null, then they are considered equal.
3. When both elements are non-null, the specified
Comparator
determines the order.
4. If specified comparator is null, then the returned comparator considers all non-null elements equal.
Using Comparator.nullsFirst
Find the example to useComparator.nullsFirst
method.
NullsFirstDemo.java
package com.concretepage; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class NullsFirstDemo { public static void main(String[] args) { Student s1 = new Student("Ram", 18); Student s2 = new Student("Shyam", 22); Student s3 = new Student("Mohan", 17); System.out.println("-------Case1: One null----------"); List<Student> list = Arrays.asList(s1, s2, null, s3); Collections.sort(list, Comparator.nullsFirst(Comparator.comparing(Student::getName))); list.forEach(s -> System.out.println(s)); System.out.println("--------Case2: More than one null---------"); list = Arrays.asList(s1, null, s2, null, s3); Collections.sort(list, Comparator.nullsFirst(Comparator.comparing(Student::getName))); list.forEach(s -> System.out.println(s)); System.out.println("--------Case3: Reverse specified Comparator to nullsFirst---------"); list = Arrays.asList(s1, null, s2, null, s3); Collections.sort(list, Comparator.nullsFirst(Comparator.comparing(Student::getName).reversed())); list.forEach(s -> System.out.println(s)); System.out.println("--------Case4: Reverse Comparator returned by nullsFirst---------"); list = Arrays.asList(s1, null, s2, null, s3); Collections.sort(list, Comparator.nullsFirst(Comparator.comparing(Student::getName)).reversed()); list.forEach(s -> System.out.println(s)); System.out.println("--------Case5: Specify natural order Comparator to nullsFirst---------"); list = Arrays.asList(s1, null, s2, null, s3); Collections.sort(list, Comparator.nullsFirst(Comparator.naturalOrder())); list.forEach(s -> System.out.println(s)); System.out.println("--------Case6: Specify null to nullsFirst---------"); list = Arrays.asList(s1, null, s2, null, s3); Collections.sort(list, Comparator.nullsFirst(null)); list.forEach(s -> System.out.println(s)); } }
package com.concretepage; public class Student implements Comparable<Student> { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public int compareTo(Student o) { return name.compareTo(o.getName()); } @Override public String toString(){ return name + "-" + age; } }
-------Case1: One null---------- null Mohan-17 Ram-18 Shyam-22 --------Case2: More than one null--------- null null Mohan-17 Ram-18 Shyam-22 --------Case3: Reverse specified Comparator to nullsFirst--------- null null Shyam-22 Ram-18 Mohan-17 --------Case4: Reverse Comparator returned by nullsFirst--------- Shyam-22 Ram-18 Mohan-17 null null --------Case5: Specify natural order Comparator to nullsFirst--------- null null Mohan-17 Ram-18 Shyam-22 --------Case6: Specify null to nullsFirst--------- null null Ram-18 Shyam-22 Mohan-17
Case-1: We have one null element in our collection. In sorting order null element will be first because of the comparator returned by
nullsFirst
method. Non-null elements order will be determined by the comparator passed to nullsFirst
method.
Case-2: We have more than one null element. As we know that when both elements are null, then they are considered equal. So all the null elements will be first in the order. Non-null elements order will be determined by the comparator passed to
nullsFirst
method.
Case-3: In this case we are reversing specified comparator to
nullsFirst
method. This will affect only the order of non-null elements. All the null elements will be first in the order.
Case-4: In this case we are reversing comparator returned by
nullsFirst
method. Now all the null elements will be last in the order.
Case-5: In this case we are passing comparator to use natural order of elements. For natural order, the element class needs to implement
Comparable
and override compareTo
method. All null elements will be first in order and non-null elements will be in its natural order.
Case-6: In this case we are passing null to
nullsFirst
method. As we know that if specified comparator to nullsFirst
is null, then the returned comparator considers all non-null elements equal. All the null elements will be first in the order and there will be no impact on order of non-null elements.
References
Interface ComparatorJava Comparator.comparing