$show=/label

Java 8 Comparator Lambda Examples

SHARE:

A quick guide to use comparator with lambda expressions in java 8.

1. Overview


In this tutorial, We'll learn how to use the Comparator interface with lambda expression in Java 8 onwards.

Lambda expression is to replace the anonymous implementations and to reduce the boilerplate code.

This makes the code cleaner and focus more on the business logic rather than semantics.


Java community provided a few sets of rules and guidelines in creating the lambda statements.

Lambda syntax and invalid one are discussed in detail in the previous discussions "java 8 lambda expressions".

First, we will see the examples without lambda and then the next section will see the equivalent lambdas.

For sorting to work with lambda comparator, we have to use the Collections.sort() method.

Java Comparator Lambda Examples



2. Usage of Java Comparator Without Lambda


Comparator in Java is an interface that is used to sort the collections based on the custom objects fields.

The comparator must be used with the combination of List or Set or Queue implementations.

Here the comparator creation is called an Anonymous comparator.

2.1 Sort Engineers by int id


Simple example 1
package com.javaprogramto.java8.comparator.lambda;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class WIthoutLambdasExample {

	public static void main(String[] args) {

		List<Engineer> engineers1 = getEngineersList();

		// printing engineer objects sorted by id
		System.out.println("Before sorting");
		engineers1.forEach(System.out::println);

		// comparator before java 8 and without lambds
		Comparator<Engineer> idComparator = new Comparator<Engineer>() {

			@Override
			public int compare(Engineer e1, Engineer e2) {

				return Integer.valueOf(e1.getId()).compareTo(e2.getId());
			}
		};

		// sorting with collections.sort()
		Collections.sort(engineers1, idComparator);

		// printing engineer objects sorted by id
		System.out.println("\nAfter sorting");
		engineers1.forEach(System.out::println);

	}

	private static List<Engineer> getEngineersList() {
		// creating the List of engineers
		List<Engineer> engineers = new LinkedList<>();

		// adding engineer objects
		engineers.add(new Engineer(100, "Jeo", 100000, false));
		engineers.add(new Engineer(103, "Sunny", 500000, true));
		engineers.add(new Engineer(104, "Zeon", 300000, false));
		engineers.add(new Engineer(105, "Neon", 400000, true));
		engineers.add(new Engineer(102, "Ammon", 200000, false));

		return engineers;
	}

}

class Engineer {

	private int id;
	private String name;
	private long salary;
	private boolean fullTime;

	public Engineer(int id, String name, long salary, boolean fullTime) {
		super();
		this.id = id;
		this.name = name;
		this.salary = salary;
		this.fullTime = fullTime;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getSalary() {
		return salary;
	}

	public void setSalary(long salary) {
		this.salary = salary;
	}

	public boolean isFullTime() {
		return fullTime;
	}

	public void setFullTime(boolean fullTime) {
		this.fullTime = fullTime;
	}

	@Override
	public String toString() {
		return "Engineer [id=" + id + ", name=" + name + ", salary=" + salary + ", fullTime=" + fullTime + "]";
	}
}

Output
Before sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]

After sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]


2.2 Sort Engineers by string name



Example 2
// sort engineers by name

List<Engineer> engineers2 = getEngineersList();

// printing engineer objects sorted by id
System.out.println("Before sorting");
engineers2.forEach(System.out::println);

// comparator before java 8 and without lambds
Comparator<Engineer> nameComparator = new Comparator<Engineer>() {

	@Override
	public int compare(Engineer e1, Engineer e2) {

		return e1.getName().compareTo(e2.getName());
	}
};

// sorting with collections.sort()
Collections.sort(engineers2, nameComparator);

// printing engineer objects sorted by id
System.out.println("\nAfter sorting");
engineers2.forEach(System.out::println);

Output
Before sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]

After sorting
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]


2.3 Sort Engineers by Long Salary


Example 3
// sort engineers by salary

List<Engineer> engineers3 = getEngineersList();

// printing engineer objects sorted by id
System.out.println("\nEngieer Sort by Salary");
System.out.println("\nBefore sorting");
engineers3.forEach(System.out::println);

// comparator before java 8 and without lambds
Comparator<Engineer> salaryComparator = new Comparator<Engineer>() {

	@Override
	public int compare(Engineer e1, Engineer e2) {

return Long.valueOf(e1.getSalary()).compareTo(e2.getSalary());
	}
};

// sorting with collections.sort()
Collections.sort(engineers3, salaryComparator);

// printing engineer objects sorted by id
System.out.println("\nAfter sorting");
engineers3.forEach(System.out::println);

Output
Engieer Sort by Salary

Before sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]

After sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]


2.4 Sort Engineers by Boolean  FullTime


Example 4
// sort engineers by full time

List<Engineer> engineers4 = getEngineersList();

// printing engineer objects sorted by id
System.out.println("\nEngieer Sort by fulltime");
System.out.println("\nBefore sorting");
engineers4.forEach(System.out::println);

// comparator before java 8 and without lambds
Comparator<Engineer> fulltimeComparator = new Comparator<Engineer>() {

	@Override
	public int compare(Engineer e1, Engineer e2) {

return Boolean.valueOf(e1.isFullTime()).compareTo(Boolean.valueOf(e2.isFullTime()));
	}
};

// sorting with collections.sort()
Collections.sort(engineers4, fulltimeComparator);

// printing engineer objects sorted by id
System.out.println("\nAfter sorting");
engineers4.forEach(System.out::println);

Output
Engieer Sort by fulltime

Before sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]

After sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]


3. Usage Lambda With Java Comparator


By using lambda, we can reduce code and make focusing much on core logic. 

Lambda is indicated with the arrow symbol (->). Look at the below syntax

(datatype variable1,datatype variable2, ..) -> { logic }


Here the data types are optional and no need to use the return statement inside the lambda body if there is only one statement.

When working with lambda or java 8 then you do not need to go with the Collections.sort() method.
because java 8 list is added with the sort() method directly. So, we can call list.sort() with the comparator instance or lambda.

3.1 Compartor sort by id with Lambda


below is the complete example with Lambda and list.sort() to sort the list of engineers by id in ascending order.



Example 5
public class WithLambdasExample {

	public static void main(String[] args) {

		List<Engineer> engineers1 = getEngineersList();

		// printing engineer objects sorted by id
		System.out.println("Before sorting");
		engineers1.forEach(System.out::println);

		// comparator with lambdas
		Comparator<Engineer> idComparator = (e1, e2) -> Integer.valueOf(e1.getId()).compareTo(e2.getId()); 

		// sorting with list.sort()
		engineers1.sort(idComparator);

		// printing engineer objects sorted by id
		System.out.println("\nAfter sorting");
		engineers1.forEach(System.out::println);

	}

	private static List<Engineer> getEngineersList() {
		// creating the List of engineers
		List<Engineer> engineers = new LinkedList<>();

		// adding engineer objects
		engineers.add(new Engineer(100, "Jeo", 100000, false));
		engineers.add(new Engineer(103, "Sunny", 500000, true));
		engineers.add(new Engineer(104, "Zeon", 300000, false));
		engineers.add(new Engineer(105, "Neon", 400000, true));
		engineers.add(new Engineer(102, "Ammon", 200000, false));

		return engineers;
	}

}

Output
Before sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]

After sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]

3.2 Compartor sort by name with Lambda


Example 6
// comparator in java 8 with lambdas
Comparator<Engineer> nameComparator = (e1, e2) -> e1.getName().compareTo(e2.getName());

// sorting with list.sort()
engineers2.sort(nameComparator);

Output
Before sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]

After sorting
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]

3.3 Compartor sort by salary with Lambda


Example 7
// comparator in java 8 with lambds
Comparator<Engineer> salaryComparator = (e1, e2) -> Long.valueOf(e1.getSalary()).compareTo(e2.getSalary());

// sorting with list.sort()
engineers3.sort(salaryComparator);

Output
Engieer Sort by Salary

Before sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]

After sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]

3.4 Comparator sort by full time with Lambda


Example 8
// comparator in java 8 with lambdas
Comparator<Engineer> fulltimeComparator = (e1, e2) -> Boolean.valueOf(e1.isFullTime()).compareTo(Boolean.valueOf(e2.isFullTime()));

// sorting with list.sort()
engineers4.sort(fulltimeComparator);

Output
Engieer Sort by fulltime

Before sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]

After sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]

4. Java 8 Comparator with Lambda sorting by multiple fields


The below example is to sort based on the full time and id.

First, create two separate comparators with lambda by full time and id.

Next, call thenComparint() method on the full-time comparator as below.


Example 9
Comparator<Engineer> fullTimeAndIdComparator = fulltimeComparator.thenComparing(idComparator);

This creates a new comparator with by full time and id fields together.

Look at the full code for multiple fields.

Full Example
public class WithLambdasExample2 {

	public static void main(String[] args) {

		List<Engineer> engineers1 = getEngineersList();

		// printing engineer objects sorted by id
		System.out.println("Before sorting");
		engineers1.forEach(System.out::println);

		// comparator with lambdas
		Comparator<Engineer> fulltimeComparator = (e1, e2) -> Boolean.valueOf(e1.isFullTime())
				.compareTo(Boolean.valueOf(e2.isFullTime()));

		Comparator<Engineer> idComparator = (e1, e2) -> Integer.valueOf(e1.getId()).compareTo(e2.getId());

		Comparator<Engineer> fullTimeAndIdComparator = fulltimeComparator.thenComparing(idComparator);

		// sorting with collections.sort()
		engineers1.sort(fullTimeAndIdComparator);

		// printing engineer objects sorted by id
		System.out.println("\nAfter sorting");
		engineers1.forEach(System.out::println);

	}
}

Output
Before sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]

After sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]


5. Java 8 Comparator with Lambda Sorting in reverse order or descending order


To get sorted the objects with comparator by reverse order and objects in the descending order.

For the descending order, after creating the Comparator with lambda then you need to call reversed() method of Comparator as below.

Example 10
Comparator<Engineer> fullTimeAndIdComparator = fulltimeComparator.thenComparing(idComparator).reversed();

// sorting with collections.sort()
engineers1.sort(fullTimeAndIdComparator);

// printing engineer objects sorted by id
System.out.println("\nAfter sorting");
engineers1.forEach(System.out::println);

Output
Before sorting
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]

After sorting
Engineer [id=105, name=Neon, salary=400000, fullTime=true]
Engineer [id=103, name=Sunny, salary=500000, fullTime=true]
Engineer [id=104, name=Zeon, salary=300000, fullTime=false]
Engineer [id=102, name=Ammon, salary=200000, fullTime=false]
Engineer [id=100, name=Jeo, salary=100000, fullTime=false]

Compare this output with the above section output for the reversed order.


6. Conclusion


In this article, We've seen how to use lambda with comparator in java 8.



COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java 8 Comparator Lambda Examples
Java 8 Comparator Lambda Examples
A quick guide to use comparator with lambda expressions in java 8.
https://blogger.googleusercontent.com/img/a/AVvXsEhT1dE0R2SiIbkxphg0q2gnEhNzIq9lUXYDff0Abxgi0VDLyjM6letX00D5f07FLfKAg0Ay-tHQIvInpjCifLKOsSAtnVuHUreAzAQuxQK0ReIDZemgUlW4A5Q3kbF9BK2iJmL8ZffiBxfU3l01qO99INdaLwlq_m9GxXmeH7mzsEIH3TqtfYcsmIpN=w400-h240
https://blogger.googleusercontent.com/img/a/AVvXsEhT1dE0R2SiIbkxphg0q2gnEhNzIq9lUXYDff0Abxgi0VDLyjM6letX00D5f07FLfKAg0Ay-tHQIvInpjCifLKOsSAtnVuHUreAzAQuxQK0ReIDZemgUlW4A5Q3kbF9BK2iJmL8ZffiBxfU3l01qO99INdaLwlq_m9GxXmeH7mzsEIH3TqtfYcsmIpN=s72-w400-c-h240
JavaProgramTo.com
https://www.javaprogramto.com/2021/12/java-comparator-lambda.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2021/12/java-comparator-lambda.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content