In this example we take a look at the Predicate Functional Interface to filter a List of Employee objects based on a condition where salary is > 40k.
package org.code2care.examples;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
class Employee {
private String empName;
private int empAge;
private double empSalary;
public Employee(String name, int age, double salary) {
this.empName = name;
this.empAge = age;
this.empSalary = salary;
}
public String getEmpName() {
return empName;
}
public int getEmpAge() {
return empAge;
}
public double getEmpSalary() {
return empSalary;
}
}
public class EmployeePredicateExample {
public static void main(String... args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("Andy", 21, 40000.0));
employees.add(new Employee("Sharma", 25, 71000.0));
employees.add(new Employee("Luke", 22, 42000.0));
employees.add(new Employee("Mikey", 24, 56000.0));
// Predicate to filter employees with a salary above 40k
Predicate<Employee> highSalaryFilter = employee -> employee.getEmpSalary() > 40000.0;
List<Employee> highSalaryEmployees = filterEmployees(employees, highSalaryFilter);
System.out.println("Employees with High Salary:");
for (Employee employee : highSalaryEmployees) {
System.out.println("Name: " + employee.getEmpName() + ", Age: " + employee.getEmpAge() + ", Salary: $" + employee.getEmpSalary());
}
}
private static List<Employee> filterEmployees(List<Employee> employees, Predicate<Employee> predicate) {
List<Employee> filteredList = new ArrayList<>();
for (Employee employee : employees) {
if (predicate.test(employee)) {
filteredList.add(employee);
}
}
return filteredList;
}
}
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:
- Python: SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers - Python
- Get Device Screen Width and Height using javaScript - JavaScript
- Docker - Error response from daemon: You cannot remove a running container - Docker
- Fix: Notepad++ bottom status bar not visible - NotepadPlusPlus
- Python: How to Save Image from URL - Python
- VS Code Remove Unused Imports Keyboard Shortcut - HowTos
- How to Unzip a file using Python - Python
- How to Join Microsoft Teams Meeting Without an Account - Teams