Java Program To Find Second Highest Salary in the Organisation

1. Introduction

This tutorial guides you through the process of writing a Java program to find the second-highest salary in an organization. Identifying the second-highest salary can be useful for benchmarking compensation levels and ensuring competitive salary offerings.

Key Points

- Using a custom Employee class with a salary attribute.

- Employing Java's Stream API to process and sort salaries.

- Extracting the second highest salary from a sorted list of unique salaries.

2. Program Steps

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

2. Populate a list of Employee instances.

3. Sort the salaries and find the second-highest salary using Java's Stream API.

4. Print the second highest salary.

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;
    }
}

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

public class FindSecondHighestSalary {
    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),
            new Employee(6, "Rohit", 29, 150000, "M", "Engineering", "Bangalore", 1999)
        );

        long secondHighestSalary = employees.stream()
                                            .mapToLong(Employee::getSalary)
                                            .boxed()
                                            .distinct()
                                            .sorted(Comparator.reverseOrder())
                                            .skip(1)
                                            .findFirst()
                                            .orElse(0);

        System.out.println("The second highest salary in the organization is: " + secondHighestSalary);
    }
}

Output:

The second highest salary in the organization is: 130000

Explanation:

1. The Employee class includes a salary attribute, allowing the program to access each employee's salary.

2. In the FindSecondHighestSalary main method, a list of Employee objects is created, which includes potentially duplicate salary values.

3. The stream().mapToLong().boxed() chain converts long salaries to Long objects, allowing them to be collected into a list that is then sorted in descending order and made distinct.

4. The skip(1) method is used to bypass the highest salary, and findFirst() fetches the second highest.

5. The second highest salary is printed to the console. If not enough unique salaries exist, it defaults to zero.

Comments