Find Highest Salary Employee For Each Department Using Java 8 Streams

Introduction:

In today's data-driven world, the ability to efficiently process and analyze large datasets is paramount. Java, being one of the most widely used programming languages, offers powerful tools to tackle such tasks. One of these tools is Java Streams, introduced in Java 8, which provides a functional approach to processing collections of data. In this blog post, we'll explore how we can utilize Java Streams to find the highest salary for each department in a company.

Java Streams to Find the Highest Salaried Employee in each Department
Java Streams to Find the Highest Salaried Employee in each Department


Imagine we have a collection of employees, each belonging to a specific department and having a corresponding salary. Our goal is to find the highest salary for each department. Traditionally, this task might involve nested loops or complex logic. However, with Java Streams, we can achieve this in a concise and elegant manner.


advertisement


Let's dive into the implementation:

import java.util.*;
import java.util.stream.Collectors;

class Employee {
    private String name;
    private String department;
    private double salary;

    public Employee(String name, String department, double salary) {
        this.name = name;
        this.department = department;
        this.salary = salary;
    }

    public String getDepartment() {
        return department;
    }

    public double getSalary() {
        return salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", department='" + department + '\'' +
                ", salary=" + salary +
                '}';
    }
}

public class Main {
    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
                new Employee("John", "HR", 50000),
                new Employee("Alice", "IT", 60000),
                new Employee("Bob", "HR", 55000),
                new Employee("Jane", "IT", 62000),
                new Employee("Eva", "Finance", 70000),
                new Employee("Michael", "Finance", 75000)
        );

        Map<String, Optional<Employee>> highestSalariesByDept = employees.stream()
                .collect(Collectors.groupingBy(Employee::getDepartment,
                        Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary))));

        // Print the result
        highestSalariesByDept.forEach((dept, employee) -> {
            System.out.println("Department: " + dept +
                    ", Highest Salary: " + (employee.isPresent() ? employee.get().getSalary() : "N/A"));
        });
    }
}


In this code snippet, we define an Employee class representing an individual employee with attributes such as name, department, and salary. We then create a list of Employee objects to simulate our dataset.

The magic happens in the main method where we use Java Streams to process the list of employees. We first group the employees by their departments using Collectors.groupingBy. Within each department group, we find the employee with the maximum salary using Collectors.maxBy. Finally, we print out the result, displaying the department along with its highest salary.

By leveraging Java Streams and its functional programming features, we have succinctly solved the problem of finding the highest salary for each department. This approach not only simplifies the code but also makes it more readable and maintainable.


advertisement

Conclusion:


In conclusion, Java Streams provide a powerful toolkit for processing collections of data in a functional style. By utilizing its capabilities, we can efficiently solve complex tasks such as finding the highest salary for each department with ease.

Post a Comment

Previous Post Next Post