Comparator comparingDouble() method in Java with examples
The comparingDouble(java.util.function.ToDoubleFunction) method of Comparator Interface in Java accepts a function as parameter that extracts an double sort key from a type T, and returns a Comparator that compares by that sort key.The returned comparator is serializable if the specified function is also serializable.
Syntax:
static <T> Comparator<T> comparingDouble(
ToDoubleFunction <T> keyExtractor)
Parameters: This method accepts a single parameter keyExtractor which is the function used to extract the double sort key.
Return value: This method returns a comparator that compares by an extracted key
Exception: This method throws NullPointerException if the argument is null.
Below programs illustrate comparingDouble(java.util.function.ToDoubleFunction) method:
Program 1:
// Java program to demonstrate Comparator // comparingDouble(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) { // create some user objects User u1 = new User("Aaman", 3500.56); User u2 = new User("Joyita", 4222.99); User u3 = new User("Suvam", 2832.34); User u4 = new User("mahafuj", 2522.76); // before sort List<User> list = Arrays.asList(u2, u1, u4, u3); System.out.println("Before Sort:"); list.forEach(user -> System.out.println(user)); Collections.sort(list, Comparator.comparingDouble(User::getSalary)); System.out.println("\nAfterSort:"); list.forEach(user -> System.out.println(user)); } } class User implements Comparable<User> { public String name; public double salary; public User(String name, double d) { this.name = name; this.salary = d; } public int compareTo(User u1) { return name.compareTo(u1.name); } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return "User [name=" + name + ", salary=" + salary + "]"; } } |
The output printed on console of IDE is shown below.
Output:

Program 2:
// Java program to demonstrate Comparator // comparingDouble(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) { // before sort List<Order> list = Arrays.asList( new Order("A382y482y48", 32684.82), new Order("Vvekhfbkje2", 28348.47), new Order("efkhfbekjfbe", 47674.49), new Order("bhdhdfaddvad", 78449.43), new Order("abkasbcjabjc", 10104.43)); System.out.println("Before Sort:"); list.forEach(order -> System.out.println(order)); Collections.sort(list, Comparator.comparingDouble(Order::getValue)); System.out.println("\nAfter Sort:"); list.forEach(order -> System.out.println(order)); } } class Order implements Comparable<Order> { public String orderNo; public double value; public int compareTo(Order o1) { return orderNo.compareTo(o1.orderNo); } public Order(String orderNo, double value) { super(); this.orderNo = orderNo; this.value = value; } @Override public String toString() { return "Order [orderNo=" + orderNo + ", value=" + value + "]"; } public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public double getValue() { return value; } public void setValue(double value) { this.value = value; } } |
The output printed on console is shown below.
Output:

Recommended Posts:
- Java | Collectors minBy(Comparator comparator) with Examples
- Java | Collectors maxBy(Comparator comparator) with Examples
- Stream sorted (Comparator comparator) method in Java
- Comparator comparingLong() method in Java with examples
- Comparator reversed() method in Java with examples
- SortedMap comparator() method in Java with Examples
- Comparator nullsLast() method in Java with examples
- Comparator thenComparingInt() method in Java with examples
- Comparator reverseOrder() method in Java with examples
- Comparator nullsFirst() method in Java with examples
- Comparator thenComparingDouble() method in Java with examples
- Comparator naturalOrder() method in Java with examples
- TreeMap comparator() method in Java with Examples
- Comparator thenComparingLong() method in Java with examples
- ConcurrentSkipListSet comparator() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



