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:
1 2 3 4 5 6 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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.
1 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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: