Java 8 Example: Sorting Employees by Multiple Properties

Here’s an example of an Employee class with properties such as employeeId, name, address, and pincode. We will demonstrate how to sort a list of employees based on all three properties using Java 8 features like lambda expressions and the Comparator interface.

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

class Employee {
    private int employeeId;
    private String name;
    private String address;
    private String pincode;

    public Employee(int employeeId, String name, String address, String pincode) {
        this.employeeId = employeeId;
        this.name = name;
        this.address = address;
        this.pincode = pincode;
    }

    public int getEmployeeId() {
        return employeeId;
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public String getPincode() {
        return pincode;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "employeeId=" + employeeId +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", pincode='" + pincode + '\'' +
                '}';
    }
}

public class EmployeeSortingExample {
    public static void main(String[] args) {
        List employees = new ArrayList<>();
        employees.add(new Employee(2, "John", "123 Main St", "10001"));
        employees.add(new Employee(1, "Jane", "456 Elm St", "20002"));
        employees.add(new Employee(4, "Adam", "789 Oak St", "30003"));
        employees.add(new Employee(3, "Eva", "987 Maple St", "40004"));

        // Sort employees by employeeId
        employees.sort(Comparator.comparing(Employee::getEmployeeId));
        System.out.println("Sorted by employeeId:");
        employees.forEach(System.out::println);
        System.out.println();

        // Sort employees by name
        employees.sort(Comparator.comparing(Employee::getName));
        System.out.println("Sorted by name:");
        employees.forEach(System.out::println);
        System.out.println();

        // Sort employees by address
        employees.sort(Comparator.comparing(Employee::getAddress));
        System.out.println("Sorted by address:");
        employees.forEach(System.out::println);
        System.out.println();

        // Sort employees by pincode
        employees.sort(Comparator.comparing(Employee::getPincode));
        System.out.println("Sorted by pincode:");
        employees.forEach(System.out::println);
        System.out.println();

        // Sort employees by employeeId, name, and pincode
        employees.sort(Comparator.comparing(Employee::getEmployeeId)
                .thenComparing(Employee::getName)
                .thenComparing(Employee::getPincode));
        System.out.println("Sorted by employeeId, name, and pincode:");
        employees.forEach(System.out::println);
    }
}

Output:

Sorted by employeeId:
Employee{employeeId=1, name='Jane', address='456 Elm St', pincode='20002'}
Employee{employeeId=2, name='John', address='123 Main St', pincode='10001'}
Employee{employeeId=3, name='Eva', address='987 Maple St', pincode='40004'}
Employee{employeeId=4, name='Adam', address='789 Oak St', pincode='30003'}

Sorted by name:
Employee{employeeId=4, name='Adam', address='789 Oak St', pincode='30003'}
Employee{employeeId=3, name='Eva', address='987 Maple St', pincode='400

04'}
Employee{employeeId=1, name='Jane', address='456 Elm St', pincode='20002'}
Employee{employeeId=2, name='John', address='123 Main St', pincode='10001'}

Sorted by address:
Employee{employeeId=4, name='Adam', address='789 Oak St', pincode='30003'}
Employee{employeeId=3, name='Eva', address='987 Maple St', pincode='40004'}
Employee{employeeId=1, name='Jane', address='456 Elm St', pincode='20002'}
Employee{employeeId=2, name='John', address='123 Main St', pincode='10001'}

Sorted by pincode:
Employee{employeeId=2, name='John', address='123 Main St', pincode='10001'}
Employee{employeeId=1, name='Jane', address='456 Elm St', pincode='20002'}
Employee{employeeId=4, name='Adam', address='789 Oak St', pincode='30003'}
Employee{employeeId=3, name='Eva', address='987 Maple St', pincode='40004'}

Sorted by employeeId, name, and pincode:
Employee{employeeId=1, name='Jane', address='456 Elm St', pincode='20002'}
Employee{employeeId=2, name='John', address='123 Main St', pincode='10001'}
Employee{employeeId=4, name='Adam', address='789 Oak St', pincode='30003'}
Employee{employeeId=3, name='Eva', address='987 Maple St', pincode='40004'}

In the example, we create an Employee class with its properties and implement the necessary methods. Then, we create a list of employees and demonstrate how to sort them based on different properties using the Comparator interface and lambda expressions. Finally, we show an example of sorting by multiple properties using the thenComparing method of the Comparator interface.

Happy Learning.

Leave a Reply

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