Comparator nullsFirst() method in Java with examples Last Updated : 29 Apr, 2019 Comments Improve Suggest changes Like Article Like Report The nullsFirst(java.util.Comparator) method returns comparator that is a null-friendly comparator and considers null values to be less than non-null. The null first operates by the following logic: The null element is considered to be less than non-null. When both elements are null, then they are considered equal. When both elements are non-null, the specified Comparator determines the order. If the specified comparator is null, then the returned comparator considers all non-null elements equal. The returned comparator is serializable if the specified comparator is serializable. Syntax: static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) Parameters: This method accepts a single parameter comparator which is a Comparator for comparing non-null values Return value: This method returns a comparator that considers null to be less than non-null and compares non-null objects with the supplied Comparator. Below programs illustrate nullsFirst(java.util.Comparator) method: Program 1: Java // Java program to demonstrate // Comparator.nullsFirst(java.util.Comparator) method import java.util.Arrays; import java.util.Comparator; public class GFG { public static void main(String[] args) { // Create a collection of an array // of names also contain nulls String[] strings = { "aman", "suvam", null, "sahil", null }; // print the array System.out.println("Before sorting: " + Arrays.toString(strings)); // apply nullsFirst method // and sort the array Arrays.sort(strings, Comparator.nullsFirst( Comparator.naturalOrder())); // print the array System.out.println("After sorting: " + Arrays.toString(strings)); } } The output printed on console of IDE is shown below. Output: Program 2: Java // Java program to demonstrate // Comparator.nullsFirst(java.util.Comparator) method import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class GFG { public static void main(String[] args) { // Create some user objects User u1 = new User("Aaman", 25); User u2 = new User("Joyita", 22); User u3 = new User("Suvam", 28); User u4 = new User("mahafuj", 25); System.out.println("One null Objects"); List<User> list = Arrays.asList(u1, u2, u3, null, u4); Collections.sort(list, Comparator.nullsFirst( Comparator.comparing( User::getName))); list.forEach(user -> System.out.println(user)); System.out.println("\nMore than One null Objects"); list = Arrays.asList(u1, u4, null, u2, u3, null, null); Collections.sort(list, Comparator.nullsFirst( Comparator.comparing( User::getName))); list.forEach(user -> System.out.println(user)); } } class User implements Comparable<User> { public String name; public int age; public User(String name, int age) { this.name = name; this.age = age; } public int compareTo(User u1) { return name.compareTo(u1.name); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } } The output printed on console is shown below. Output: References: https://docs.oracle.com/javase/10/docs/api/java/util/Comparator.html#nullsFirst(java.util.Comparator) Comment More infoAdvertise with us Next Article Comparator nullsFirst() method in Java with examples A AmanSingh2210 Follow Improve Article Tags : Java Java - util package Java-Functions Java-Comparator Practice Tags : Java Similar Reads Comparator nullsLast() method in Java with examples The nullsLast (java.util.Comparator) method returns comparator that is a null-friendly comparator and considers null values greater than non-null. The null first operates by the following logic: The null element is considered to be greater than non-null. When both elements are null, then they are co 3 min read TreeSet comparator() Method in Java with Examples TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to co 3 min read Comparator naturalOrder() method in Java with examples The naturalOrder() method of Comparator Interface in Java returns a comparator that use to compare Comparable objects in natural order. The returned comparator by this method is serializable and throws NullPointerException when comparing null. Syntax: static <T extends Comparable<T>> Com 1 min read TreeMap comparator() method in Java with Examples The comparator() method of java.util.TreeMap class is used to return the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys. --> java.util Package --> TreeMap Class --> comparator() Method Syntax: public Comparator comparator() Return Ty 2 min read Comparator reverseOrder() method in Java with examples The reverseOrder() method of Comparator Interface in Java returns a comparator that use to compare Comparable objects in reverse of natural order. The returned comparator by this method is serializable and throws NullPointerException when comparing null. Syntax: static <T extends Comparable<T 1 min read Comparator thenComparingInt() method in Java with examples The thenComparingInt(java.util.function.ToIntFunction) method of Comparator Interface in Java returns a lexicographic-order comparator with a function that extracts a int sort key. Syntax: default Comparator <T> thenComparingInt(ToIntFunction <T> keyExtractor) Parameters: This method acc 2 min read OptionalInt empty() method in Java with examples OptionalInt help us to create an object which may or may not contain a int value. The empty() method returns an empty OptionalInt instance. No value is present for this OptionalInt. So we can say that this method help us to create empty OptionalInt object. Syntax: public static OptionalInt empty() P 1 min read RuleBasedCollator compare() method in Java with Example The compare() method of java.text.RuleBasedCollator class is used to compare the strength of two objects and it will return 0, positive and negative value as an output according to the result. Syntax: public int compare(Object o1, Object o2) Parameter: This method takes two objects between which com 2 min read Optional empty() method in Java with examples The empty() method of java.util.Optional class in Java is used to get an empty instance of this Optional class. This instance do not contain any value. Syntax: public static <T> Optional<T> empty() Parameters: This method accepts nothing. Return value: This method returns an empty instan 1 min read Collections min() method in Java with Examples min(Collection<? extends T> coll) The min() method of java.util.Collections class is used to return the minimum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements 4 min read Like