Problem Statement: Implement a Java program to sort an ArrayList of custom objects based on a specific attribute.
Here’s a Java program that demonstrates how to sort an ArrayList of custom objects (in this case, Employee objects) based on a specific attribute (name in this case). I’ll provide an explanation of the program’s logic.
package com.javacodepoint.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
// User defined class
class Employee {
private int id;
private String name;
private int age;
public Employee(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
public class SortCustomObjectsArrayList {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(101, "Alice", 28));
employees.add(new Employee(102, "Bob", 32));
employees.add(new Employee(103, "Eve", 25));
employees.add(new Employee(104, "David", 30));
System.out.println("Original List:");
for (Employee employee : employees) {
System.out.println(employee);
}
// Sorting the list based on name attribute
Collections.sort(employees, Comparator.comparing(Employee::getName));
System.out.println("\nSorted List (by name):");
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
OUTPUT:
Original List:
Employee [id=101, name=Alice, age=28]
Employee [id=102, name=Bob, age=32]
Employee [id=103, name=Eve, age=25]
Employee [id=104, name=David, age=30]
Sorted List (by name):
Employee [id=101, name=Alice, age=28]
Employee [id=102, name=Bob, age=32]
Employee [id=104, name=David, age=30]
Employee [id=103, name=Eve, age=25]
Explanation:
- Custom Employee Class: We define a class named
Employeewith attributesid,name, andage, along with a constructor, getter methods, and atoString()method for better representation. - Creating the List: We create an
ArrayListnamedemployeesand populate it withEmployeeobjects, each representing an employee with unique attributes. - Printing the Original List: We iterate through the
employeeslist and print the details of each employee using thetoString()method. - Sorting the List: We use
Collections.sort()to sort theemployeeslist based on thenameattribute. We achieve this by providing a custom comparator using theComparator.comparing()method and specifying the getter method for the attribute to be used as the sorting key. - Printing the Sorted List: After sorting, we iterate through the sorted
employeeslist and print the details of each employee again.
By using Comparator.comparing() and specifying the getName() method as the sorting key, the program sorts the ArrayList of Employee objects based on the name attribute. This approach can be extended to sort custom objects based on any attribute.
See also: Find duplicate elements in an ArrayList.
