In this post, we will show you two different approaches to perform a collection sorting. One way is through traditional way by using Comparator class and in next example, we will see how to use Lambda expression that came into existence since Java 8 to sort the collections and making the code much elegant and declarative. In this example, we will see “How to replace a classic Comparator to sort a collection with Java 8 Lambda expression?”.
Easiest representation of collections sorting using Java 8 Lambda...!!! Share on X
In this tutorial, we are going to cover below topics:
- Collection sorting with classic Comparator (solution before Java 8)
- Collection sorting using lambda expression (solution after Java 8)
Let’s begin:
1. Collection sorting using Comparator (without Lambda)
Comparator Syntax:
Collections.sort(listOfStudents, new Comparator < Student > () {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge();
}
});
Example:
In this example, we are sorting the list by age using classic comparator.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestSortWithoutLambda {
public static void main(String[] args) {
List < Student > listOfStudents = getStudents();
System.out.println("Before Sorting:\n");
for (Student Student: listOfStudents) {
System.out.println(Student.getAge() + "---" + Student.getName());
}
//sorting by age
Collections.sort(listOfStudents, new Comparator < Student > () {
@Override
public int compare(Student s1, Student s2) {
return s1.getAge() - s2.getAge();
}
});
System.out.println("\nAfter Sorting:\n");
for (Student Student: listOfStudents) {
System.out.println(Student.getAge() + "---" + Student.getName());
}
}
static List < Student > getStudents() {
List < Student > students = new ArrayList < Student > ();
students.add(new Student(5, "yashu", 33));
students.add(new Student(64, "gaurangee", 20));
students.add(new Student(22, "pulkit", 10));
students.add(new Student(93, "isha", 5));
students.add(new Student(8, "deepak", 77));
students.add(new Student(44, "yogi", 1));
students.add(new Student(87, "yashi", 22));
return students;
}
}
class Student {
int rollno, age;
String name;
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int rollno, String name,
int age) {
this.rollno = rollno;
this.name = name;
this.age = age;
}
}
Output:
Before Sorting: 33---yashu 20---gaurangee 10---pulkit 5---isha 77---deepak 1---yogi 22---yashi After Sorting: 1---yogi 5---isha 10---pulkit 20---gaurangee 22---yashi 33---yashu 77---deepak
2. Collections sorting using Lambda
Lambda syntax:
Sort the list directly using Lambda expression in Java 8. There is no need to pass the list into collections object before comparing it anymore.
listOfStudents.sort((Student s1, Student s2)->s1.getAge()-s2.getAge());
Example:
In this example, we are sorting the list by age using Java 8 Lambda expression.
import java.util.ArrayList;
import java.util.List;
public class TestSortWithLambda {
public static void main(String[] args) {
List < Student > listOfStudents = getStudents();
System.out.println("Before Sorting:\n");
for (Student student: listOfStudents) {
System.out.println(student.getAge() + "---" + student.getName());
}
System.out.println("\nAfter Sorting:\n");
//Sort the list directly using Lambda expression
listOfStudents.sort((Student s1, Student s2) - > s1.getAge() - s2.getAge());
//Print the sorted list using Java 8 foreach method
listOfStudents.forEach((student) - > System.out.println(student.getAge() + "---" + student.getName()));
}
static List < Student > getStudents() {
List < Student > students = new ArrayList < Student > ();
students.add(new Student(5, "yashu", 33));
students.add(new Student(64, "gaurangee", 20));
students.add(new Student(22, "pulkit", 10));
students.add(new Student(93, "isha", 5));
students.add(new Student(8, "deepak", 77));
students.add(new Student(44, "yogi", 1));
students.add(new Student(87, "yashi", 22));
return students;
}
}
class Student {
int rollno, age;
String name;
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int rollno, String name,
int age) {
this.rollno = rollno;
this.name = name;
this.age = age;
}
}
Output:
Before Sorting: 33---yashu 20---gaurangee 10---pulkit 5---isha 77---deepak 1---yogi 22---yashi After Sorting: 1---yogi 5---isha 10---pulkit 20---gaurangee 22---yashi 33---yashu 77---deepak
See, how elegant and powerful Java 8 Lambda is.
Easiest representation of collections sort using Java 8 Lambda...!!! Share on X
Do you like this Post? – then check my other helpful posts: