Java 8 – Find the Highest Salary in the Organization

Introduction

Java 8 introduced the Stream API, which provides a powerful way to process collections of data in a functional style. A common requirement in data processing is to find the maximum value in a collection, such as identifying the employee with the highest salary in an organization. The Stream API, combined with methods like max and Comparator, makes this task straightforward and efficient.

In this guide, we will explore how to use Java 8 streams to find and print the highest salary in an organization.

Table of Contents

  • Problem Statement
  • Solution Steps
  • Java Program
    • Example: Finding and Printing the 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 highest salary among all employees in the organization.

Example:

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

Solution Steps

  1. Create an Employee Class: Define an Employee class with fields such as name and salary.
  2. Find the Highest Salary: Use the max method and Comparator to find the employee with the highest salary.
  3. Handle the Result: Properly handle the result, including potential empty lists.
  4. Print the Result: Display the employee with the highest salary.

Java Program

Example: Finding and Printing the 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;
import java.util.Comparator;

/**
 * Java 8 - Find the Highest Salary in the Organization
 * Author: https://www.rameshfadatare.com/
 */
public class HighestSalaryExample {

    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 highest salary
        Optional<Employee> highestSalaryEmployee = employees.stream()
            .max(Comparator.comparingDouble(Employee::getSalary));

        // Print the employee with the highest salary
        if (highestSalaryEmployee.isPresent()) {
            Employee employee = highestSalaryEmployee.get();
            System.out.println("Employee with the highest salary: " + employee.getName() + 
                               ", Salary: " + employee.getSalary());
        } else {
            System.out.println("No employees found.");
        }
    }
}

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 highest salary: Raj, Salary: 70000.0

Explanation

  • Employee Class: This class includes fields name and salary. The getSalary() method retrieves the salary of each employee.
  • max(Comparator.comparingDouble(Employee::getSalary)): This method finds the employee with the highest salary using a comparator that compares employees based on their salary.
  • Optional<Employee>: The result is wrapped in an Optional to handle the case where the list might be empty. If an employee is found, their name and salary are printed.

Conclusion

Using Java 8’s Stream API, finding and printing the highest salary in an organization is both efficient and straightforward. The combination of max and Comparator allows you to quickly identify the employee with the maximum salary. This approach can be adapted to find other maximum values, such as the highest experience or the most significant performance metrics, making the Stream API used for data analysis in Java.

Leave a Comment

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

Scroll to Top