In this tutorial, we will learn how to sort a list using using Comparator interface and Lambda expression. Rewrite Traditional For Loops with Stream and Lambda expression in Java
Sort the List of Objects using Comparator and Lambda expression in Java 8 Share on X
Let’s consider we have a list of objects, and we want to sort them based on a specific attribute. Here’s an example using the Comparator
interface and lambda expressions in Java:
Assuming we have a class Student
with attributes name
and age
:
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 | import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * Using Java 8, Sort the List of Objects using Comparator and Lambda expression * @author Deepak Verma * */ public class Sort_List_of_Objects_using_Comparator_and_Lambda_expression_Java8 { public static void main(String[] args) { // Create a list of Student objects List<Student> studentList = new ArrayList<>(); studentList.add(new Student("Yashi", 19)); studentList.add(new Student("Isha", 33)); studentList.add(new Student("Gaurangee", 21)); studentList.add(new Student("Yashu", 18)); studentList.add(new Student("Deepak", 36)); // Sort the list based on age using Comparator and lambda expression Collections.sort(studentList, Comparator.comparingInt(Student::getAge)); // Print the sorted list studentList.forEach(student -> System.out.println(student.getName() + " - " + student.getAge())); } } class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } |
In the above example:
- The
Student
class has aname
andage
. - The
Comparator.comparingInt(Student::getAge)
creates a comparator that comparesStudent
objects based on theirage
attribute. Collections.sort(studentList, ...)
sorts thestudentList
using the specified comparator.
Output:
1 2 3 4 5 | Yashu - 18 Yashi - 19 Gaurangee - 21 Isha - 33 Deepak - 36 |
find first non repeated character in a string using Java 8
It demonstrates how we can use the Comparator
interface and lambda expressions to sort a list of objects based on a specific attribute.
Sort the List using Comparator and Lambda in Java 8..!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: