Comparator thenComparingDouble() method in Java with examples
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.
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:
The output printed on console of IDE is shown below.
You can see in example first sorting is done on school wise and if the school is same then percentageMarks wise.
The output printed on console is shown below.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.