w3resource

Java Program: Sort list of objects with lambda expression


20. Sort objects by attribute using lambda

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.

For more Practice: Solve these Related Problems:

  • Write a Java program to implement a lambda expression that sorts a list of objects by a numerical attribute in descending order.
  • Write a Java program to create a lambda that sorts a list of objects by a string attribute ignoring case differences.
  • Write a Java program to implement a lambda expression that sorts objects by multiple attributes using Comparator chaining.
  • Write a Java program to chain lambda expressions to sort a list of objects by one attribute and then filter the sorted list based on a condition.

Go to:


Improve this sample solution and post your code through Disqus

PREV : Find 2nd largest/smallest in array using lambda.
NEXT : Sum all primes in range using lambda.

Live Demo:

Java Code Editor:

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.