The lambda expression is a part of Java version 8. This expression is responsible for taking some parameters and returning the value for those parameters. The lambda expressions in Java are quite similar to the methods but the lambda expressions do not require a specific name and they are usually declared within a method’s body. The comparators are used to compare the objects with each other.
This write-up will specifically discuss the working of Lambda expressions with respect to writing comparators in Java.
How to Write Comparators as Lambda Expressions in Java?
The comparator in Java refers to the interface that basically deals with the sorting of objects based on the comparisons between the objects. The lambda expressions in Java are usually compatible/adaptable with a functional interface. The comparator is a functional interface, so it can be utilized to write comparators as lambda expressions. The lambda expression contains the number of parameters and returns values the same as that of the method.
Example: Implementation of Comparators as Lambda Expressions in Java
This code takes certain parameters and compares those parameters using the lambda expression.
//Import the util package of Java
import java.util.*;
//Create a student class
class student {
//Enter the specific parameters like id, name, and percentage.
int id;
String name;
double percentage;
public student(int id, String name, double percentage) {
//The super() keyword refers to the parent class.
super();
this.id = id;
this.name = name;
this.percentage = percentage;
}
}
//The lambda constructor class
class lamba {
//The main class of Java
public static void main(String[] args) {
List<student> newlist = new ArrayList<student>();
// Add students along with their ID, name, and percentages
newlist.add(new student(12, "Adam", 85));
newlist.add(new student(89, "Mary", 80));
newlist.add(new student(15, "Chris",88));
newlist.add(new student(28, "Lily", 83));
//This statement will print the students based on their names
System.out.println("SORTING THE STUDENTS ON THE BASIS OF NAME:");
//The lambda expression for comparison according to the names
Collections.sort(newlist, (s1, s2) -> {
return s1.name.compareTo(s2.name);
});
for(student s : newlist) {
//Print statement to print the output.
System.out.println("Id is "+s.id + " name is " + s.name + " with percentage of " + s.percentage);
}
}
}
In the above Java code:
- A class named student is declared.
- This class contains the parameters like ID, Name, and percentage of students.
- The super() keyword is used/imported to refer to the parent class.
- The lambda constructor class consists of an ArrayList that contains the ID, Name, and percentage of students.
- The lambda expression is employed to perform comparison according to the names.
- The sort() method compares the string name and prints the sorted names in alphabetic order.
- The println() method prints the sorted result.
Output
In the below output, the name that starts with the alphabet “A” has been printed first, since the sort() method compared the names with each other in the list under the lambda constructor of Java.

This brings us to the end of using comparators to write the lambda expressions in Java.
Conclusion
To write comparators as Lambada expressions in Java the functional interface is used. Comparators have been used as lambda expressions to sort and then compare the values to print the results. In this write-up, we have implemented a way to write comparators as lambda expressions in Java in an easy and efficient manner.