🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Comparator.html
Comparator (Java Platform SE 8 )
October 20, 2025 - For example, suppose one adds two elements a and b such that (a.equals(b) && c.compare(a, b) != 0) to an empty TreeSet with comparator c. The second add operation will return true (and the size of the tree set will increase) because a and b are not equivalent from the tree set's perspective, ...
🌐
Medium
medium.com › @AlexanderObregon › javas-comparator-comparing-method-explained-342361288af6
Java’s Comparator.comparing() Method Explained | Medium
October 2, 2024 - In this article, we’ll explore the comparing() method, its syntax, and how it can be used to sort collections based on specific object fields such as name, date, and price. We will also discuss performance considerations and provide code examples to demonstrate practical usage. The Comparator.comparing() method belongs to the java...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-comparator-interface
Java Comparator Interface - GeeksforGeeks
Student class includes name and age with getter methods. StudentComparator compares by name first, then by age if names are same. Collections.sort() uses this comparator to order students. Final list shows sorted order by both name and age fields. Java 8 introduced a more simple way to write comparators using lambda expressions.
Published   April 20, 2016
🌐
Baeldung
baeldung.com › home › java › guide to java comparator.comparing()
Guide to Java Comparator.comparing() | Baeldung
January 8, 2024 - In this tutorial, we’ll explore several functions introduced for the Comparator interface in Java 8. For the examples in this tutorial, let’s create an Employee bean and use its fields for comparing and sorting purposes:
🌐
Baeldung
baeldung.com › home › java › core java › comparator and comparable in java
Comparator and Comparable in Java | Baeldung
March 26, 2025 - To create a Comparator, we have to implement the Comparator interface. For our first example, we’ll create a Comparator to use the ranking attribute of Player to sort the players:
🌐
W3Schools
w3schools.com › java › java_advanced_sorting.asp
Java Advanced Sorting (Comparator and Comparable)
For example, if you have a list of cars you might want to sort them by year, the rule could be that cars with an earlier year go first. The Comparator and Comparable interfaces allow you to specify what rule is used to sort objects.
🌐
DigitalOcean
digitalocean.com › community › tutorials › comparable-and-comparator-in-java-example
Comparable and Comparator in Java Example | DigitalOcean
August 3, 2022 - Comparator interface compare(Object o1, Object o2) method need to be implemented that takes two Object argument, it should be implemented in such a way that it returns negative int if the first argument is less than the second one and returns zero if they are equal and positive int if the first ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › comparable-vs-comparator-in-java
Java Comparable vs Comparator - GeeksforGeeks
August 18, 2025 - Functional Interface were introduced in Java 8. It has exactly one abstract method. In Comparator<T>, the only abstract method is: int compare(T o1, T o2);
🌐
GeeksforGeeks
geeksforgeeks.org › java › comparator-comparingint-in-java-with-examples
Comparator comparingInt() in Java with examples - GeeksforGeeks
July 11, 2025 - // Java program to demonstrate // Comparator.comparingInt(java.util.function.ToIntFunction) 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); // before sort List<User> list = Arrays.asList(u2, u1, u4, u3); System.out.println("Before Sort:"); list.forEach(User -> System.out.println("User age " + User.getAge()));
Find elsewhere
🌐
Blogger
javarevisited.blogspot.com › 2021 › 09 › comparator-comparing-thenComparing-example-java-.html
Java 8 Comparator comparing() and thenComparing() Example - Tutorial
In this example, we have three comparators, the first comparator compares Student object on firstName, second compare them on both firstName and lastName, and the third comparator compares them on both name and age.
🌐
Medium
medium.com › @AlexanderObregon › javas-comparator-thencomparing-method-explained-988e8f926a64
Java’s Comparator.thenComparing() Explained | Medium
November 6, 2024 - The sequence of criteria is key in multi-level sorting. Place your primary comparison first, followed by secondary, tertiary, and other criteria. For example, sorting a list of products by category and, within each category, by price:
Top answer
1 of 4
14

is Comparator.comparing() used for converting a single argument lambda expression to a double argument?

Yes, you can sort of think of it like that.

When sorting things, you are supposed to specify "given two things a and b, which of them is greater, or are they equal?" using a Comparator<T>. The a and b is why it has 2 lambda parameters, and you return an integer indicating your answer to that question.

However, a much more convenient way to do this is to specify "given a thing x, what part of x do you want to sort by?". And that is what you can do with the keyExtractor argument of Comparator.comparing.

Compare:

/*
given two people, a and b, the comparison result between a and b is the 
comparison result between a's name and b's name
*/
Comparator<Person> personNameComparator = 
    (a, b) -> a.getName().compareTo(b.getName());

/*
given a person x, compare their name
*/
Comparator<Person> personNameComparator = 
    Comparator.comparing(x -> x.getName()); // or Person::getName

The latter is clearly much more concise and intuitive. We tend to think about what things to sort by, rather than how exactly to compare two things, and the exact number to return depending on the comparison result.

As for the declaration for comparing:

public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
            Function<? super T, ? extends U> keyExtractor)

The <T, U extends Comparable<? super U>> part first declares two generic type parameters - T is what the comparator compares (Person in the above case), and U is the type that you are actually comparing (String in the above case), hence it extends Comparable.

keyExtractor is the parameter you pass in, such as x -> x.getName(), that should answer the question of "when given a T, what is a U that you want to compare by?".

If you are confused by the ? super and ? extends, read What is PECS?.

If you haven't realised already, the implementation of comparing basically boils down to:

return (a, b) -> keyExtractor.apply(a).compareTo(keyExtractor.apply(b));
2 of 4
7

Comparator#compare(T o1, T o2) Compare two objects and returns an integer value based on this criteria:

  • A negative value if o1 < o2
  • A positive value if o1 > o2
  • Zero if they are equal.

Comparator.comparing(Function<? super T, ? extends U> key) returns a Comparator<T> that compares by that sort key.

The main difference is that compare method provides a single point of comparison, whereas comparing chained to other functions to provide multiple points of comparison.

Suppose you have a class Person

public class Person implements Comparable<Person> {
    private String firstName;
    private String lastName;
    private int age;
    // rest of class omitted
}

if you compare two Person instances p1 vs p2 using compare(p1, p2), the comparison will be executed and the two objects will be sorted based on some natural ordering prescribed by the class. In contrast, if you want to compare the same two instances using comparing(), the comparison will be executed based on whichever criteria you choose to compare based on some attribute of the class. For example: Comparator.comparing(Person::getFirstName).

Because comparing returns a Comparator rather than a value, as I stated before, you can chain multiple comparisons. For instance: Comparator.comparing(Person::getLastName).thenComparing(Person::getFirstName);

As for the meaning of the return type <T, U extends Comparable<? super U>> Comparator<T>, you can find the explanation here.

I want to add that, classes must be comparable in order for compare(T o1, T o2) to work. String objects are comparable because they implement this interface. That said, if a class is not Comparable, you can still use comparing method because as I stated, you get to choose which attribute of the class you would like to use for the comparison and those attributes are likely to be comparable (i.e. String in the case of person's name or age in the above example).

🌐
Javabrahman
javabrahman.com › java-8 › the-complete-java-8-comparator-tutorial-with-examples
The Complete Java 8 Comparator Tutorial with examples
Employee Name:null Age:45 Employee Name:null Age:65 Employee Name:Deborah Sprightly Age:29 Employee Name:Harry Major Age:35 Employee Name:Harry Major Age:25 Employee Name:Nancy Smith Age:15 Keeping the employee list same and invoking the Comparator using nullLast() - Java 8 Comparator with sort key null and using nullsLast()
🌐
Tutorialspoint
tutorialspoint.com › home › java › java comparator example
Java Comparator Example
September 1, 2008 - Java Vs. C++ ... Both TreeSet and TreeMap store elements in sorted order. However, it is the comparator that defines precisely what sorted order means.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › util › Comparator.html
Comparator (Java SE 17 & JDK 17)
January 20, 2026 - For example, suppose one adds two elements a and b such that (a.equals(b) && c.compare(a, b) != 0) to an empty TreeSet with comparator c. The second add operation will return true (and the size of the tree set will increase) because a and b are not equivalent from the tree set's perspective, ...
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › util › Comparator.html
Comparator (Java Platform SE 6)
For example, suppose one adds two elements a and b such that (a.equals(b) && c.compare(a, b) != 0) to an empty TreeSet with comparator c. The second add operation will return true (and the size of the tree set will increase) because a and b are not equivalent from the tree set's perspective, ...
🌐
DEV Community
dev.to › sohailshah › different-ways-to-use-comparator-interface-in-java-4mgj
Different Ways To Use Comparator Interface in Java - DEV Community
July 15, 2023 - In this post, we will explore various ways to use the Comparator interface in Java to compare objects and sort collections · Let's assume we have a list of Student objects, with a Student class that has some member variables. We'll use this as an example through the post.
🌐
HappyCoders.eu
happycoders.eu › java › comparator-comparable-compareto
compareTo, Comparable, Comparator - Comparing Objects in Java
June 12, 2025 - Using a Comparator, we can sort objects in an order other than their natural order. And we can sort objects that do not implement the Comparable interface, i.e., that do not have a natural order. Since Java 8, comparators can be defined very elegantly, as for example in students.sort(Comparator.comparing(Student::getLastName).
🌐
Codecademy
codecademy.com › docs › java › comparator
Java | Comparator | Codecademy
July 28, 2025 - In this example, the Java Comparator class named SalaryComparator is a reusable class for comparing employees based on their salary and Collections.sort() applies the comparator to arrange employees in ascending order:
🌐
LabEx
labex.io › tutorials › java-comparator-and-comparable-117394
Mastering Java Comparator and Comparable | LabEx
The Comparator interface allows us to define custom ordering logic that is separate from the class being compared. This means we can have multiple ways to sort the same class of objects. Let's create a Comparator that sorts students by their names alphabetically: Create a new file named StudentNameComparator.java in the ~/project directory with the following code: