Java Program To Sort the Employees Salary in the Organisation in Ascending Order

1. Introduction

This tutorial demonstrates how to write a Java program that sorts the salaries of employees in ascending order. Sorting salaries can help in payroll analysis, budget planning, and ensuring fairness in salary distribution across an organization.

Key Points

- Use of a custom Employee class with a salary attribute.

- Employing Java's Stream API to sort salaries in ascending order.

- Printing sorted salaries for review or further processing.

2. Program Steps

1. Define the Employee class with necessary attributes, including salary.

2. Create a list of Employee instances.

3. Sort the list by salary using Java's Stream API.

4. Print the sorted salaries.

3. Code Program

// File: Employee.java
public class Employee {
    private int id;
    private String name;
    private int age;
    private long salary;
    private String gender;
    private String deptName;
    private String city;
    private int yearOfJoining;

    public Employee(int id, String name, int age, long salary, String gender, String deptName, String city, int yearOfJoining) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.gender = gender;
        this.deptName = deptName;
        this.city = city;
        this.yearOfJoining = yearOfJoining;
    }

    public long getSalary() {
        return salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
               "id=" + id +
               ", name='" + name + '\'' +
               ", age=" + age +
               ", salary=" + salary +
               ", gender='" + gender + '\'' +
               ", deptName='" + deptName + '\'' +
               ", city='" + city + '\'' +
               ", yearOfJoining=" + yearOfJoining +
               '}';
    }
}

// File: SortSalaries.java
import java.util.*;
import java.util.stream.Collectors;

public class SortSalaries {
    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
            new Employee(1, "Aditi", 30, 100000, "F", "HR", "Mumbai", 1995),
            new Employee(2, "Rahul", 25, 130000, "M", "Engineering", "Bangalore", 2000),
            new Employee(3, "Vishal", 34, 110000, "M", "Engineering", "Mumbai", 1998),
            new Employee(4, "Lakshmi", 28, 150000, "F", "HR", "Bangalore", 1992),
            new Employee(5, "Priya", 24, 90000, "F", "Marketing", "Delhi", 2005)
        );

        List<Employee> sortedEmployees = employees.stream()
                                                   .sorted(Comparator.comparingLong(Employee::getSalary))
                                                   .collect(Collectors.toList());

        sortedEmployees.forEach(System.out::println);
    }
}

Output:

Employee{id=5, name='Priya', age=24, salary=90000, gender='F', deptName='Marketing', city='Delhi', yearOfJoining=2005}
Employee{id=1, name='Aditi', age=30, salary=100000, gender='F', deptName='HR', city='Mumbai', yearOfJoining=1995}
Employee{id=3, name='Vishal', age=34, salary=110000, gender='M', deptName='Engineering', city='Mumbai', yearOfJoining=1998}
Employee{id=2, name='Rahul', age=25, salary=130000, gender='M', deptName='Engineering', city='Bangalore', yearOfJoining=2000}
Employee{id=4, name='Lakshmi', age=28, salary=150000, gender='F', deptName='HR', city='Bangalore', yearOfJoining=1992}

Explanation:

1. An Employee class is defined with a salary field to hold each employee's salary information.

2. The SortSalaries class creates a list of Employee objects and uses Java Streams to sort them by salary in ascending order.

3. The Comparator.comparingLong method is used within the sorted method to dictate the sorting order based on salaries.

4. Finally, the sorted list of employees is printed out, showing each employee in order of their salary from lowest to highest.

Comments