Java 8 – Find the Second Highest Salary in the Organization

Introduction

Java 8 introduced the Stream API, which allows for powerful data processing operations on collections in a functional style. One common requirement in many applications is finding the second-highest salary among employees in an organization. This task involves sorting the salaries and then identifying the second-highest value. The Stream API, combined with methods like sorted, distinct, and skip, makes this task straightforward and efficient.

In this guide, we’ll explore how to use Java 8 streams to find the second-highest salary in an organization.

Table of Contents

  • Problem Statement
  • Solution Steps
  • Java Program
    • Example: Finding the Second-Highest Salary in the Organization
  • Conclusion

Problem Statement

Given a list of Employee objects, each containing information about the employee’s name and salary, you need to find the second-highest salary among all employees in the organization.

Example:

  • Problem: Identify the employee with the second-highest salary in the organization.
  • Goal: Use Java 8’s Stream API to efficiently find and display the second-highest salary.

Solution Steps

  1. Create an Employee Class: Define an Employee class with fields such as name and salary.
  2. Sort Salaries in Descending Order: Use the sorted method to sort the salaries in descending order.
  3. Skip the Highest Salary: Use the skip method to bypass the highest salary.
  4. Find the Second-Highest Salary: Use the findFirst method to get the second-highest salary.
  5. Handle the Result: Properly handle the result, including potential empty lists.
  6. Print the Result: Display the second-highest salary and the corresponding employee’s name.

Java Program

Example: Finding the Second-Highest Salary in the Organization

First, define the Employee class with the required fields.

import java.util.Arrays; import java.util.List; import java.util.Optional; /** * Java 8 - Find the Second Highest Salary in the Organization * Author: https://www.rameshfadatare.com/ */ public class SecondHighestSalaryExample { public static void main(String[] args) { List<Employee> employees = Arrays.asList( new Employee("Amit", 50000), new Employee("Priya", 60000), new Employee("Raj", 70000), new Employee("Suman", 55000), new Employee("Kiran", 65000) ); // Find the employee with the second-highest salary Optional<Employee> secondHighestSalaryEmployee = employees.stream() .sorted((e1, e2) -> Double.compare(e2.getSalary(), e1.getSalary())) // Sort by salary in descending order .distinct() // Ensure no duplicate salaries are considered .skip(1) // Skip the highest salary .findFirst(); // Get the second-highest salary // Print the employee with the second-highest salary if (secondHighestSalaryEmployee.isPresent()) { Employee employee = secondHighestSalaryEmployee.get(); System.out.println("Employee with the second-highest salary: " + employee.getName() + ", Salary: " + employee.getSalary()); } else { System.out.println("No employees found or there is no second-highest salary."); } } } class Employee { private String name; private double salary; public Employee(String name, double salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public double getSalary() { return salary; } } 

Output

Employee with the second-highest salary: Kiran, Salary: 65000.0 

Explanation

  • Employee Class: This class includes fields name and salary. The getSalary() method retrieves the salary of each employee.
  • sorted((e1, e2) -> Double.compare(e2.getSalary(), e1.getSalary())): This method sorts the employees by salary in descending order.
  • distinct(): This method ensures that only unique salary values are considered, avoiding issues with duplicate salaries.
  • skip(1): This method skips the first (highest) salary in the sorted list, allowing the next salary to be selected.
  • findFirst(): This method retrieves the first element in the stream after the skip operation, which corresponds to the second-highest salary.
  • Optional<Employee>: The result is wrapped in an Optional to handle cases where there might not be a second-highest salary (e.g., if all employees have the same salary or the list is too small).

Conclusion

Using Java 8’s Stream API, finding and printing the second-highest salary in an organization is efficient and straightforward. By combining sorting, distinct filtering, skipping, and retrieving the first result, the task is handled in a clean and functional manner.

Leave a Comment

Scroll to Top