Sorting a List: sort an ArrayList of custom objects

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:

  1. Custom Employee Class: We define a class named Employee with attributes id, name, and age, along with a constructor, getter methods, and a toString() method for better representation.
  2. Creating the List: We create an ArrayList named employees and populate it with Employee objects, each representing an employee with unique attributes.
  3. Printing the Original List: We iterate through the employees list and print the details of each employee using the toString() method.
  4. Sorting the List: We use Collections.sort() to sort the employees list based on the name attribute. We achieve this by providing a custom comparator using the Comparator.comparing() method and specifying the getter method for the attribute to be used as the sorting key.
  5. Printing the Sorted List: After sorting, we iterate through the sorted employees list 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.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *