🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Comparator.html
Comparator (Java Platform SE 8 )
October 20, 2025 - The returned comparator is serializable if the specified comparator is also serializable. ... For example, to sort a collection of String based on the length and then case-insensitive natural ordering, the comparator can be composed using following code,
🌐
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:
🌐
Javabrahman
javabrahman.com › java-8 › the-complete-java-8-comparator-tutorial-with-examples
The Complete Java 8 Comparator Tutorial with examples
Java 8's Comparator as an assignment target for LambdaExpressions Given the fact that its a functional interface, an instance of a Comparator can now be created in Java 8 with a lambda expression specifying its comparison logic. Take a look at the code snippet below - ... package com.javabrahman.java8.comparator; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import com.javabrahman.java8.Employee; public class ComparatorsInJava8 { static List<Employee> employeeList = Arrays.asList(new Employee("Tom Jones", 45), new Employee("Harry Maj
🌐
Mkyong
mkyong.com › home › java8 › java 8 lambda : comparator example
Java 8 Lambda : Comparator example - Mkyong.com
August 5, 2015 - Actually, we can do it even simplier. Instead of writing for example · Comparator salaryComparator = (o1, o2)->o1.getSalary().compareTo(o2.getSalary()); listDevs.sort(salaryComparator.reversed());
🌐
Readthedocs
java8tips.readthedocs.io › en › stable › comparator.html
7. Comparator — Java 8 tips 1.0 documentation
It is very much possible that two elements will be equal according to the given comparator. In such cases the other comprator decides the sorting order. Below code snippet shows example of sorting employee objects based on employee’s salary and then uses name if two salaries are equal.
🌐
Baeldung
baeldung.com › home › java › core java › comparator and comparable in java
Comparator and Comparable in Java | Baeldung
March 26, 2025 - Java 8 provides new ways of defining Comparators by using lambda expressions, and the comparing() static factory method. Let’s see a quick example of how to use a lambda expression to create a Comparator:
🌐
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.
🌐
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   April 20, 2016
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java comparator with lambda
Java Comparator with Lambda (with Examples) - HowToDoInJava
February 6, 2023 - Learn to create a Comparator instance with lambda expressions, method references and chaining multiple comparators for complex comparisons. Lokesh Gupta · February 6, 2023 · Java 8 · Java 8, Java Compare, Lambda Expression · The Comparator interface is used to sort a collection of objects that can be compared.
Find elsewhere
🌐
Blogger
javarevisited.blogspot.com › 2023 › 04 › 7-examples-of-comparator-and-comparable.html
7 Examples of Comparator and Comparable in Java 8
Hello guys, you may know that Java 8 introduced a number of features to simplify the code and make it more readable. One such feature is enhancements of Comparator and Comparable interfaces. The Comparator interface provides a way to compare two objects and sort them based on specific criteria. The Comparable interface is used to compare the objects of the same class and sort them based on their natural order. In this article, we'll explore 7 different examples of Comparator and Comparable in Java 8.
🌐
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
If you are amazed to see the previous example of Comparator, you will be even more surprised to see this example, which demonstrates another magic of Java 8 which allows you to chain methods to perform sophisticated comparison logic. One of the common requirements in the real world is to compare objects by multiple fields like the first compare by the author and if the author is same the as compare by title or price.
🌐
Readthedocs
java-8-tips.readthedocs.io › en › stable › comparator.html
7. Comparator — Java 8 tips 1.0 documentation
It is very much possible that two elements will be equal according to the given comparator. In such cases the other comprator decides the sorting order. Below code snippet shows example of sorting employee objects based on employee’s salary and then uses name if two salaries are equal.
🌐
Java67
java67.com › 2014 › 11 › java-8-comparator-example-using-lambda-expression.html
How to implement Comparator and Comparable in Java with Lambda Expression & method reference? Example | Java67
It has just one abstract method compare() which means you can pass a lambda expression where a Comparator is expected. Many Java programmers often ask me, what is the best way to learn lambda expression of Java 8? And, my answer is, of course by using it on your day to the day programming task.
🌐
Java67
java67.com › 2023 › 10 › 10-example-of-comparator-and-sorting-in.html
10 Examples of Comparator, Comparable, and Sorting in Java 8 | Java67
That's all about sorting and Comparator in Java 8. In these 10 examples, you can see how much has changed on Comparator and how you can use new Java 8 features like lambda expression, method reference and new API methods from Comparator class to write short and effective Comparator in Java 8.
🌐
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.
🌐
Medium
medium.com › @AlexanderObregon › javas-comparator-comparing-method-explained-342361288af6
Java’s Comparator.comparing() Method Explained | Medium
October 2, 2024 - We'll go through a detailed example using a class Product, demonstrating how sorting can be done on various fields, as well as combining sorting conditions to meet specific requirements. To illustrate the power of Comparator.comparing(), we’ll work with a Product class that has three main attributes: a product name, price, and a manufacture date. Here’s how the class is structured: import java.time.LocalDate; public class Product { private String name; private double price; private LocalDate manufactureDate; public Product(String name, double price, LocalDate manufactureDate) { this.name =
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › class-use › Comparator.html
Uses of Interface java.util.Comparator (Java Platform SE 8 )
October 20, 2025 - 8 ... Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
🌐
Praveer09
praveer09.github.io › technology › 2016 › 06 › 20 › writing-comparators-the-java8-way
Writing Comparators - The Java 8 Way
Now let’s try to write a Comparator that would sort a collection of Person in the ascending order of a Person’s name. The question that would arise in your mind is that what should be sorting criteria for the null values of name? Let’s keep the Person objects, having null names, towards the end of the sorted collection. import static java.util.Comparator.nullsLast; ...
🌐
Dev.java
dev.java › learn › lambdas › writing-comparators
Writing and Combining Comparators - Dev.java
Here is an example, where you want ... for the naturalOrder() method to further improve readability): Comparator<String> byLengthThenAlphabetically = Comparator.comparing(String::length) .thenComparing(naturalOrder()); List<String> ...