Java Program: Find Name of the Employee with Maximum Salary using Stream API


Write a Java Program to find the name of the employee with the maximum salary using Stream API.

Solution:
class Employee {

    private int empId;
    private String empName;
    private String empGender;
    private int empAge;
    private int empSalary;
    private String empDepartment;

    public Employee(int empId, String empName, String empGender, int empAge, int empSalary, String empDepartment) {
        this.empId = empId;
        this.empName = empName;
        this.empGender = empGender;
        this.empAge = empAge;
        this.empSalary = empSalary;
        this.empDepartment = empDepartment;
    }

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public String getEmpGender() {
        return empGender;
    }

    public void setEmpGender(String empGender) {
        this.empGender = empGender;
    }

    public int getEmpAge() {
        return empAge;
    }

    public void setEmpAge(int empAge) {
        this.empAge = empAge;
    }

    public int getEmpSalary() {
        return empSalary;
    }

    public void setEmpSalary(int empSalary) {
        this.empSalary = empSalary;
    }

    public String getEmpDepartment() {
        return empDepartment;
    }

    public void setEmpDepartment(String empDepartment) {
        this.empDepartment = empDepartment;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "empId=" + empId +
                ", empName='" + empName + '\'' +
                ", empGender='" + empGender + '\'' +
                ", empAge=" + empAge +
                ", empSalary=" + empSalary +
                ", empDepartment='" + empDepartment + '\'' +
                '}';
    }
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

public class Main {

    public static void main(String[] args) {

        Employee employee1 = new Employee(1,"Sam","male",32,40_000,"IT");
        Employee employee2 = new Employee(2,"Alan","male",34,45_000,"Sales");
        Employee employee3 = new Employee(3,"Sara","female",31,44_000,"Finance");
        Employee employee4 = new Employee(4,"Alex","male",42,55_000,"IT");
        Employee employee5 = new Employee(5,"Andrew","male",35,30_000,"Finance");
        Employee employee6 = new Employee(6,"Tiffany","female",31,59_000,"IT");

        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(employee1);
        employeeList.add(employee2);
        employeeList.add(employee3);
        employeeList.add(employee4);
        employeeList.add(employee5);
        employeeList.add(employee6);

        Optional<Employee> employee = employeeList.stream().max(Comparator.comparingInt(Employee::getEmpSalary));

        employee.ifPresent(emp ->System.out.println("Employee with Max Salary is: " + emp.getEmpName()));


    }
}
Output:

Employee with Max Salary is: Tiffany

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap