w3resource

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:

Flowchart: Java  Exercises: Sort list of objects with lambda expression.
Flowchart: Java  Exercises: Sort list of objects with lambda expression.

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.



Follow us on Facebook and Twitter for latest update.