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:

  1. 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;  
     List<Employee> newListEmps = new ArrayList<>(); 
     Iterator<Employee> iterate = 
        employeeDaoImpl.getEmployees().iterator(); 
      while(iterate.hasNext()){ 
        Employee e = iterate.next(); 
        if(qualifiedEmps.test(e)){ 
           newListEmps.add(e); 
        } 
      }   
    return newListEmps; 
} 

Using the Predicate functional interface is one way of establishing a logical expression to be used for object validation. Its abstract method test() is implemented using lambda expression.

  1. Add this method that evaluates employee records given two series of Predicates, AND-ed, to comprise a filtering rule. This service method will fetch all qualified employees with age > 25 belonging to a certain deptId:
public List<Employee> getEmployeePerDept(int deptId){ 
      Predicate<Employee> qualifiedEmps =  
          (e) -> e.getAge() > 25; 
      Predicate<Employee> groupEmp = 
          (e) -> e.getDeptId() == deptId; 
      Predicate<Employee> rule =     
         qualifiedEmps.and(groupEmp); 
          
      List<Employee> newListEmps = new ArrayList<>(); 
      Iterator<Employee> iterate = 
        employeeDaoImpl.getEmployees().iterator(); 
      while(iterate.hasNext()){ 
         Employee e = iterate.next(); 
         if(rule.test(e)){ 
             newListEmps.add(e); 
         } 
      } 
      return newListEmps; 
} 
  1. Create another method that generates random tickets for employee complaints. This method uses the Supplier functional interface for constructing a lambda expression that only gives a value without any input:
public int employeeTicket(){ 
    Supplier<Integer> generateTicket = 
      () -> (int)(Math.random()*200000); 
    return generateTicket.get();   
} 

The Supplier functional interface has an implemented abstract method, get(), which can give the expected result generated by its lambda expression.

  1. Add the method printEmployeeNotQuaified(), which prints all employee records with the classification of whether an employee is QUALIFIED or NOT based on age:
public void printEmployeeNotQuaified(){ 
    Consumer<Employee> showNotQualified = 
      (e) ->{ 
          if(e.getAge().intValue() > 25){ 
           System.out.format("%s %s %s
",    
              e.getFirstName(), 
              e.getLastName(), "QUALIFIED"); 
          }else { 
           System.out.format("%s %s %s
",  
              e.getFirstName(), 
              e.getLastName(), "NOT QUALIFIED"); 
      } 
    }; 
     
    Iterator<Employee> iterate = 
          employeeDaoImpl.getEmployees().iterator(); 
    while(iterate.hasNext()){ 
       Employee e = iterate.next(); 
       showNotQualified.accept(e); 
    } 
}

This method highlights the use of Consumer that uses the lambda expression to accept an input of any object, but will only process transactions and not return any output. It has an implemented abstract method, accept(), that executes the lambda expression.

  1. Lastly, add the following method that uses Function to convert an object to another form. In this case, the method wants to extract only the age values in order to compute the average age of all the employees:
public double getAverageAge(){ 
   
    double avg = 0.0; 
    Function<Employee, Integer> getAge = (e) -> e.getAge(); 
    Iterator<Employee> iterate = 
       employeeDaoImpl.getEmployees().iterator(); 
    while(iterate.hasNext()){ 
       Employee e = iterate.next(); 
       avg += getAge.apply(e); 
    } 
    return avg; 
} 

The functional interface has an abstract method, apply(), which extracts the age element from the whole record.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.83.199