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!
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- CRUD operations in Spring Boot + JDBC - Java
- MySQL 1005 Error : SQLSTATE: HY000 (ER_CANT_CREATE_TABLE) Message: Can't create table '%s' (errno: 150) - MySQL
- PowerShell 1..10 foreach Example - Powershell
- How to change bash terminal prompt string and color - Linux
- Python: Fix command not found pip or pip3 on zsh shell - Python
- Java StringJoiner Class With Examples - Java
- How to rename a directory using Command Line Terminal - Linux
- Fix: SyntaxError: The requested module does not provide an export named default - JavaScript