🌐
Java67
java67.com › 2019 › 06 › top-5-sorting-examples-of-comparator-and-comparable-in-java.html
6 Advanced Comparator and Comparable Examples in Java 8 | Java67
I like books, so I'll create a Book object with some fields to demonstrate how you can sort a list of book objects by single or multiple fields using Java 8 features. ... class Book implements Comparable<Book> { private String title; private String author; private int price; public Book(String title, String author, int price) { this.title = title; this.author = author; this.price = price; } public String getTitle() { return title; } public String getAuthor() { return author; } public int getPrice() { return price; } public void setTitle(String title) { this.title = title; } public void setAuth
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Comparator.html
Comparator (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. @FunctionalInterface public interface Comparator<T>
🌐
Readthedocs
java8tips.readthedocs.io › en › stable › comparator.html
7. Comparator — Java 8 tips 1.0 documentation
A new utility class Comparators is bundled with jdk-8 to support natural ordered sorting and handling null values while sorting. Comparators is a helper class that provides new Comparator implmentations in the following cases.
🌐
Readthedocs
java-8-tips.readthedocs.io › en › stable › comparator.html
7. Comparator — Java 8 tips 1.0 documentation
A new utility class Comparators is bundled with jdk-8 to support natural ordered sorting and handling null values while sorting. Comparators is a helper class that provides new Comparator implmentations in the following cases.
🌐
Javabrahman
javabrahman.com › java-8 › the-complete-java-8-comparator-tutorial-with-examples
The Complete Java 8 Comparator Tutorial with examples
The lambda expression takes 2 Employee ... Employee list by name. Java 8 Comparator's comparing() method's working The comparing() method is a new static method introduced in Comparators in Java 8....
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java 8 tutorial › java 8 comparator
Java 8 Comparator | Examples on How to Use Java 8 Comparator
February 10, 2023 - The Java 8 comparator is used to order the objects of the class that was defined. It can be found in java.util package and includes two methods: public int compare and public Boolean equals.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Baeldung
baeldung.com › home › java › guide to java comparator.comparing()
Guide to Java Comparator.comparing() | Baeldung
January 8, 2024 - A practical guide to the static functions and instance methods of the Comparable interface that were introduced in Java 8.
🌐
Blogger
javarevisited.blogspot.com › 2021 › 09 › comparator-comparing-thenComparing-example-java-.html
Java 8 Comparator comparing() and thenComparing() Example - Tutorial
I like books, so I'll create a Book object with some fields to demonstrate how you can sort a list of books using Java 8 features like lambda expressions and method reference. ... public class Book implements Comparable < Book > { private String title; private String author; private int price; public Book(String title, String author, int price) { this.title = title; this.author = author; this.price = price; } public String getTitle() { return title; } public String getAuthor() { return author; } public int getPrice() { return price; } public void setTitle(String title) { this.title = title; }
🌐
Justamonad
justamonad.com › home › complete guide to comparator in java 8 with examples
Complete Guide to Comparator in Java 8 with examples - Platform for Object Oriented and Functional Programming Concepts
July 2, 2022 - boolean equals(Object obj) int compare(T o1, T o2) As of Java 8, Comparator interface now has 16 additional methods. That is a major overhaul. All new methods are default or public static methods. In this article, we will look at all methods with examples.
Find elsewhere
🌐
Praveer09
praveer09.github.io › technology › 2016 › 06 › 20 › writing-comparators-the-java8-way
Writing Comparators - The Java 8 Way
Java 8 introduced a few default methods and static factory methods on the Comparator interface using which developers can write Comparators in a declarative way. The Comparator interface combines the principles from Builder Pattern, Factory Method Pattern, Decorator Pattern and Functional ...
🌐
Mkyong
mkyong.com › home › java8 › java 8 lambda : comparator example
Java 8 Lambda : Comparator example - Mkyong.com
August 5, 2015 - In this example, we will show you how to use Java 8 Lambda expression to write a Comparator to sort a List.
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).

🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java comparator with lambda
Java Comparator with Lambda (with Examples) - HowToDoInJava
February 6, 2023 - The Comparator interface is used to sort a collection of objects that can be compared. The object comparison can be made using Comparable interface as well, but it restricts us by comparing objects in a specific single way only.
🌐
Medium
medium.com › @ganesh.shah › comparator-vs-comparable-java-8-940a83f53bd3
Comparator vs Comparable | Java 8 | by GANESH SHAH | Medium
March 8, 2024 - Comparator vs Comparable | Java 8 In Java, Comparator and Comparable are interfaces used for sorting objects, but they serve different purposes: Comparable Interface: The Comparable interface is …
🌐
Javatpoint
javatpoint.com › Comparator-interface-in-collection-framework
Java Comparator - javatpoint
Java Comparator interface is used to order the user-defined class objects, compare() method, collection class, java comporator example, Example of Comparator interface in collection framework.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-comparator-interface
Java Comparator Interface - GeeksforGeeks
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   November 25, 2025
🌐
Medium
medium.com › @AlexanderObregon › javas-comparator-comparing-method-explained-342361288af6
Java’s Comparator.comparing() Method Explained | Medium
October 2, 2024 - Java’s Comparator.comparing() method is a powerful tool that simplifies custom sorting based on object properties. By allowing us to create comparators for sorting collections like lists, this method reduces the complexity of implementing ...
🌐
Baeldung
baeldung.com › home › java › core java › comparator and comparable in java
Comparator and Comparable in Java | Baeldung
March 26, 2025 - If we want a different sorting ... Steven] Java 8 provides new ways of defining Comparators by using lambda expressions, and the comparing() static factory method....