Java 8 lambda for selecting top salary employee for each department

Java 8 lambda for selecting top salary employee for each department

Assuming you have a list of employees with information about their department and salary, you can use Java 8 Streams and lambda expressions to select the top salary employee for each department. Here's an example:

import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.stream.Collectors; class Employee { private String department; private String name; private double salary; public Employee(String department, String name, double salary) { this.department = department; this.name = name; this.salary = salary; } public String getDepartment() { return department; } public String getName() { return name; } public double getSalary() { return salary; } @Override public String toString() { return "Employee{" + "department='" + department + '\'' + ", name='" + name + '\'' + ", salary=" + salary + '}'; } } public class TopSalaryForEachDepartment { public static void main(String[] args) { // Sample list of employees List<Employee> employees = new ArrayList<>(); employees.add(new Employee("HR", "John", 50000)); employees.add(new Employee("HR", "Jane", 55000)); employees.add(new Employee("IT", "Bob", 60000)); employees.add(new Employee("IT", "Alice", 62000)); employees.add(new Employee("Finance", "Mike", 70000)); employees.add(new Employee("Finance", "Sara", 72000)); // Use Streams to find the top salary employee for each department Map<String, Employee> topSalaryByDepartment = employees.stream() .collect(Collectors.toMap( Employee::getDepartment, e -> e, (e1, e2) -> e1.getSalary() > e2.getSalary() ? e1 : e2 )); // Print the results topSalaryByDepartment.forEach((department, employee) -> System.out.println("Department: " + department + ", Top Salary Employee: " + employee)); } } 

In this example:

  • The Employee class represents the information about each employee, including the department, name, and salary.
  • The employees list contains instances of the Employee class.
  • The employees.stream() creates a Stream of employees.
  • The Collectors.toMap collector is used to collect the top salary employee for each department. The lambda expression (e1, e2) -> e1.getSalary() > e2.getSalary() ? e1 : e2 is used to determine which employee has the higher salary.
  • The results are printed, showing the top salary employee for each department.

You can customize the Employee class and adjust the logic based on your specific requirements.

Examples

  1. "Java 8 lambda to find top salary employee for each department"

    • Code Implementation:
      Map<String, Employee> topSalaryByDepartment = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparingInt(Employee::getSalary)), Optional::get))); 
    • Description: Use Java 8 lambda stream to find the top salary employee for each department using Collectors.groupingBy and Collectors.maxBy.
  2. "Java 8 lambda to find highest paid employee for each department"

    • Code Implementation:
      Map<String, Employee> highestPaidByDepartment = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparingInt(Employee::getSalary)), Optional::get))); 
    • Description: Find the highest paid employee for each department using Java 8 lambda stream and Collectors.groupingBy.
  3. "Java 8 lambda to get top salary employee per department"

    • Code Implementation:
      Map<String, Employee> topSalaryByDepartment = employees.stream() .collect(Collectors.toMap(Employee::getDepartment, Function.identity(), BinaryOperator.maxBy(Comparator.comparingInt(Employee::getSalary)))); 
    • Description: Use Java 8 lambda stream to get the top salary employee per department using Collectors.toMap and BinaryOperator.maxBy.
  4. "Java 8 lambda to find highest salary employee for each department"

    • Code Implementation:
      Map<String, Employee> highestSalaryByDepartment = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparingInt(Employee::getSalary)), Optional::get))); 
    • Description: Find the highest salary employee for each department using Java 8 lambda stream and Collectors.groupingBy.
  5. "Java 8 lambda to find top earning employee for each department"

    • Code Implementation:
      Map<String, Employee> topEarningByDepartment = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparingInt(Employee::getSalary)), Optional::get))); 
    • Description: Use Java 8 lambda stream to find the top earning employee for each department using Collectors.groupingBy.
  6. "Java 8 lambda to select top salary employee by department"

    • Code Implementation:
      Map<String, Employee> topSalaryByDepartment = employees.stream() .collect(Collectors.toMap(Employee::getDepartment, Function.identity(), BinaryOperator.maxBy(Comparator.comparingInt(Employee::getSalary)))); 
    • Description: Select the top salary employee for each department using Java 8 lambda stream and Collectors.toMap.
  7. "Java 8 lambda to find top paid employee for each department"

    • Code Implementation:
      Map<String, Employee> topPaidByDepartment = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparingInt(Employee::getSalary)), Optional::get))); 
    • Description: Find the top paid employee for each department using Java 8 lambda stream and Collectors.groupingBy.
  8. "Java 8 lambda to get highest salary employee per department"

    • Code Implementation:
      Map<String, Employee> highestSalaryByDepartment = employees.stream() .collect(Collectors.toMap(Employee::getDepartment, Function.identity(), BinaryOperator.maxBy(Comparator.comparingInt(Employee::getSalary)))); 
    • Description: Get the highest salary employee per department using Java 8 lambda stream and Collectors.toMap.
  9. "Java 8 lambda to find top earner employee for each department"

    • Code Implementation:
      Map<String, Employee> topEarnerByDepartment = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparingInt(Employee::getSalary)), Optional::get))); 
    • Description: Find the top earner employee for each department using Java 8 lambda stream and Collectors.groupingBy.
  10. "Java 8 lambda to select top salary employee by each department"

    • Code Implementation:
      Map<String, Employee> topSalaryByDepartment = employees.stream() .collect(Collectors.toMap(Employee::getDepartment, Function.identity(), BinaryOperator.maxBy(Comparator.comparingInt(Employee::getSalary)))); 
    • Description: Select the top salary employee for each department using Java 8 lambda stream and Collectors.toMap.

More Tags

jexcelapi filestream always-on-top random-seed npm afnetworking apache-tez svg-filters pkcs#11 fiddler

More Programming Questions

More Trees & Forestry Calculators

More Chemical thermodynamics Calculators

More Pregnancy Calculators

More Various Measurements Units Calculators