Applying the built-in functional interfaces
Java 1.8 also introduced some built-in functional interfaces that can be used directly in different lambda expressions. Predicate
, Consumer
, Supplier
, and Function
are some of the functional interfaces of the newly created package java.util.function
, which will be highlighted in this recipe.
Getting started
Using the same Eclipse project and EmployeeServiceImpl
, add the needed service methods using the pre-defined function interfaces of Java 1.8.
How to do it...
There are some built-in functional interfaces that Java 1.8 can provide in order to create services depending on the type of transactions needed to be implemented. To illustrate how to use these functional interfaces, follow these steps:
- Add another method to
EmployeeServiceImpl
that will retrieve and filter employees with an age greater than 25:
public List<Employee> getEmployeesFunc(){ Predicate<Employee> qualifiedEmps = (e) -> e.getAge() > 25; ...