Java Program: Sort list of objects with lambda expression
Java Lambda Program: Exercise-20 with Solution
Write a Java program to implement a lambda expression to sort a list of objects based on a specific attribute.
Sample Solution:
Java Code:
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Create a list of objects
List < Student > student_list = new ArrayList < > ();
student_list.add(new Student("Adriana Jamie", 15, "X"));
student_list.add(new Student("Felix Uisdean", 15, "X"));
student_list.add(new Student("Conceicao Palmira", 14, "X"));
student_list.add(new Student("Jair Camila", 14, "X"));
student_list.add(new Student("Micaela Rosana", 15, "X"));
// Student details
System.out.println("Student details:");
for (Student Student: student_list) {
System.out.println(Student.getName() + " - " + Student.getAge() + " - " + Student.getSClass());
}
// Sort the list based on age using lambda expression
student_list.sort(Comparator.comparing(Student::getName));
// Print the sorted list
System.out.println("\nSorted list based on Student Name:");
for (Student Student: student_list) {
System.out.println(Student.getName() + " - " + Student.getAge() + " - " + Student.getSClass());
}
}
}
class Student {
private String name, SClass;
private int age;
public Student(String name, int age, String SClass) {
this.name = name;
this.age = age;
this.SClass = SClass;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getSClass() {
return SClass;
}
}
Sample Output:
Student details: Adriana Jamie - 15 - X Felix Uisdean - 15 - X Conceicao Palmira - 14 - X Jair Camila - 14 - X Micaela Rosana - 15 - X Sorted list based on Student Name: Adriana Jamie - 15 - X Conceicao Palmira - 14 - X Felix Uisdean - 15 - X Jair Camila - 14 - X Micaela Rosana - 15 - X
Explanation:
In the above exercise,
- A student's name, age, and class are represented in the Student class.
- The main method creates a list called student_list to store Student objects.
- A number of Student objects are added to the student_list by using the add method.
- By using a lambda expression and the sort method, the student_list is sorted by name.
- The lambda expression Comparator.comparing(Student::getName) specifies the attribute to compare, which is the student's name in this case.
Flowchart:


Live Demo:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Java Lambda Exercises Previous: Find second largest and smallest element in an Array.
Java Lambda Exercises Next: Calculate sum of prime numbers with lambda expression.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
How do I remove repeated elements from ArrayList?
If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:
Set<String> set = new HashSet<>(yourList); yourList.clear(); yourList.addAll(set);
Of course, this destroys the ordering of the elements in the ArrayList.
Ref: https://bit.ly/3bYIjNC
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook