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:
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
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- How to crop a screenshot on Mac/Macbook - MacOS
- How to stop and start a docker container - Docker
- Minecraft Fix: Error: UNKNOWN code: Deep Ocean - Something went wrong in the login process - Windows
- Python Program To Calculate Simple Interest (SimpleInterest.py) - Python
- How to Calculate the SHA Hash Value of a File in Java - Java
- Fix - Java: invalid source release: IntelliJ IDE - Java
- bash: command not found error [macOS Terminal Linux, Unix or Windows] - MacOS
- Quick steps to install Nginx on Ubuntu Linux - Ubuntu