Comparator thenComparingDouble() method in Java with examples Last Updated : 29 Apr, 2019 Comments Improve Suggest changes Like Article Like Report The thenComparingDouble(java.util.function.ToDoubleFunction) method of Comparator Interface in Java returns a lexicographic-order comparator with a function that extracts a double sort key. This method is applied after comparing method if you want to apply another comparing for those values which are equal in the comparing method. Syntax: default Comparator <T> thenComparingDouble( ToDoubleFunction <T> keyExtractor) Parameters: This method accepts keyExtractor which is the function used to extract the Double sort key. Return value: This method returns a lexicographic-order comparator composed of this and then the Double sort key. Exception: This method throws NullPointerException if the argument is null. Below programs illustrate thenComparingDouble(java.util.function.ToDoubleFunction) method: Program 1: Java // Java program to demonstrate Comparator // thenComparingDouble(ToDoubleFunction) 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) { List<Student> list = getStudentList(); Comparator<Student> comparator = Comparator .comparing(Student::getSchool) .thenComparingDouble(Student::getpercentageMarks); Collections.sort(list, comparator); System.out.println("After sort"); list.forEach(s -> System.out.println(s)); } public static List<Student> getStudentList() { Student s1 = new Student("Ram", 85.5, "SJV"); Student s2 = new Student("Shyam", 83.25, "MSH"); Student s3 = new Student("Mohan", 86.55, "SJV"); Student s4 = new Student("Sohan", 81.00, "MSH"); Student s5 = new Student("Rabi", 55.6, "SJV"); List<Student> list = Arrays.asList(s1, s2, s3, s4, s5); return list; } } class Student { private String name; private double percentageMarks; private String school; public Student(String name, double percentageMarks, String school) { this.name = name; this.percentageMarks = percentageMarks; this.school = school; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getpercentageMarks() { return percentageMarks; } public void setpercentageMarks(int percentageMarks) { this.percentageMarks = percentageMarks; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } @Override public String toString() { return "Student [name=" + name + ", percentageMarks = " + percentageMarks + ", school=" + school + "]"; } } The output printed on console of IDE is shown below. Output: You can see in example first sorting is done on school wise and if the school is same then percentageMarks wise. Program 2: Java // Java program to demonstrate Comparator // thenComparingDouble(ToDoubleFunction) method import java.util.Arrays; import java.util.Comparator; import java.util.List; public class GFG { public static void main(String... args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6); try { // apply thenComparingDouble Comparator.comparing(list::get) .thenComparingDouble(null); } catch (Exception e) { System.out.printf("Exception:" + e); } } } The output printed on console is shown below. Output: References: https://docs.oracle.com/javase/10/docs/api/java/util/Comparator.html#thenComparingDouble(java.util.function.ToDoubleFunction)() Comment More infoAdvertise with us Next Article Comparator thenComparingDouble() 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 thenComparingLong() method in Java with examples The thenComparingLong(java.util.function.ToLongFunction) method of Comparator Interface in Java returns a lexicographic-order comparator with a function that extracts a Long sort key. Syntax: default Comparator <T> thenComparingLong( ToLongFunction <T> keyExtractor) Parameters: This meth 2 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 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 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 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 SortedMap comparator() method in Java with Examples The comparator() method of java.util.SortedMap interface 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.Syntax: public Comparator comparator() Return Value: This method returns the comparator used to order the keys in th 2 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 Short compareTo() method in Java with Examples The compareTo() method of Short class is a built in method in Java which is used to compare twoShort objects numerically. Syntax: public int compareTo(Short otherShort) Parameters : This method accepts a mandatory parameter otherShort which is the Short object to be compared. Return type : It return 2 min read 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 Comparator reversed() method in Java with examples The reversed() method of Comparator Interface in Java returns a comparator that imposes the reverse ordering of this comparator. If you use sort method of the array and passes this comparator after applying the reversed method then it will sort the array in reverse order. Syntax: default Comparator 2 min read Like