Home  >  Java 8

Java Comparator.nullsLast

By Arvind Rai, March 19, 2019
nullsLast is the static method of Comparator functional interface. Comparator.nullsLast method introduced in Java 8, returns a null-friendly comparator that considers null to be greater than non-null. Find its declaration from Java source code.
static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) 
Find the working of the comparator returned by nullsLast method.
1. The null element is considered to be greater 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.nullsLast

Find the example to use Comparator.nullsLast method.
NullsLastDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class NullsLastDemo {
  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.nullsLast(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.nullsLast(Comparator.comparing(Student::getName)));
	list.forEach(s -> System.out.println(s));

	System.out.println("--------Case3: Reverse specified Comparator to nullsLast---------");

	list = Arrays.asList(s1, null, s2, null, s3);
	Collections.sort(list, Comparator.nullsLast(Comparator.comparing(Student::getName).reversed()));
	list.forEach(s -> System.out.println(s));

	System.out.println("--------Case4: Reverse Comparator returned by nullsLast---------");

	list = Arrays.asList(s1, null, s2, null, s3);
	Collections.sort(list, Comparator.nullsLast(Comparator.comparing(Student::getName)).reversed());
	list.forEach(s -> System.out.println(s));

	System.out.println("--------Case5: Specify natural order Comparator to nullsLast---------");

	list = Arrays.asList(s1, null, s2, null, s3);
	Collections.sort(list, Comparator.nullsLast(Comparator.naturalOrder()));
	list.forEach(s -> System.out.println(s));

	System.out.println("--------Case6: Specify null to nullsLast---------");

	list = Arrays.asList(s1, null, s2, null, s3);
	Collections.sort(list, Comparator.nullsLast(null));
	list.forEach(s -> System.out.println(s));
  }
} 
Student.java
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;
  }
} 
Output
-------Case1: One null----------
Mohan-17
Ram-18
Shyam-22
null
--------Case2: More than one null---------
Mohan-17
Ram-18
Shyam-22
null
null
--------Case3: Reverse specified Comparator to nullsLast---------
Shyam-22
Ram-18
Mohan-17
null
null
--------Case4: Reverse Comparator returned by nullsLast---------
null
null
Shyam-22
Ram-18
Mohan-17
--------Case5: Specify natural order Comparator to nullsLast---------
Mohan-17
Ram-18
Shyam-22
null
null
--------Case6: Specify null to nullsLast---------
Ram-18
Shyam-22
Mohan-17
null
null 
Find the explanation case-wise.
Case-1: We have one null element in our collection. In sorting order null element will be last because of nullsLast method. Non-null elements order will be determined by the comparator passed to nullsLast 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 last in the order. Non-null elements order will be determined by the comparator passed to nullsLast method.
Case-3: In this case we are reversing specified comparator to nullsLast method. This will affect only the order of non-null elements. All the null elements will be last in the order.
Case-4: In this case we are reversing comparator returned by nullsLast method. Now all the null elements will be first 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 last in order and non-null elements will be in its natural order.
Case-6: In this case we are passing null to nullsLast method. As we know that if specified comparator to nullsLast is null, then the returned comparator considers all non-null elements equal. All the null elements will be last in the order and there will be no impact on order of non-null elements.

References

Interface Comparator
Java Comparator.comparing
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us