Monday, 5 October 2020

How to use Predicate Functional Interface in Java

How to use Predicate Functional Interface in Java 


Functional Interface :- Functional interface specifies one abstract method . The Signature of functional interface also called Function Descriptor describe the Signature of lambda expression .
  
 Predicate Interface :- Predicate is an predefined functional interface . Predicate interface have one abstract method named with test  one parameter of type T  and return boloean 
 
 public interface Predicate<T>{
 
 public boolean test(T object);
}

PredicateFunctionalInterfaceApplication .java

package in.jk.java8.predicate;

import java.util.List;
import java.util.function.Predicate;
import in.jk.java8.Employee;

public class PredicateFunctionalInterfaceApplication {
                     public static void main(String[] args) {
                      System.out.println("Predicate Functional Interface Application ............ \n");
List<Employee> employeeList = Employee.getEmployeeList();
        Predicate<Employee> salaryPredicate = (employee) -> employee.getSalary() > 5000;
employeeList.forEach(employee -> {
if (salaryPredicate.test(employee)) {
                                  System.out.println(employee);
}
});

     // Composing of Predicate
      System.out.println("\n--------- Composing of Predicate  -----------\n");
              Predicate<Employee> companyPredicate = (emp) -> emp.getCompany().equals("Google");
              Predicate<Employee> empIdPredicate = (employee) -> employee.getEmpId() == 8;
              Predicate<Employee> empIdOnePredicate = empIdPredicate.negate();

employeeList.forEach(employee -> {
if (empIdOnePredicate.test(employee)) {
System.out.println(employee);
}
});
        
               System.out.println("--------- Negate and and Predicate -----------");
Predicate<Employee> empolyeePredicate = empIdPredicate.negate()
                                                                                                                .and(companyPredicate);
               employeeList.forEach(employee -> {
if (empolyeePredicate.test(employee)) {
System.out.println(employee);
}
});
       Predicate<Employee> adobePredicate = (emp) -> emp.getCompany().equals("ADOBE");                       Predicate<Employee> empolyeePredicateComposition = empIdPredicate.negate()
                                                                                                  .and(companyPredicate)
                                                                                                  .or(adobePredicate );

System.out.println("\n--------- Use of negate ,and ,or  Predicate -----------\n");
employeeList.forEach(employee -> {
if (empolyeePredicateComposition.test(employee)) {
System.out.println(employee);
}
});
}
}

Employee.java 

package in.jk.java8;

import java.util.ArrayList;
import java.util.List;

public class Employee {
private int empId;
private String name;
private String company;
private int salary;
//Getters and Setters
@Override
public String toString() {
return "Employee [empId=" + empId + ", name=" + name + ", company=" + company + ",                      salary=" + salary + "]";
}
public static  List<Employee>getEmployeeList() {
List<Employee> empList =new ArrayList<Employee>();
Employee  employee = new Employee();
        employee.setEmpId(1);
        employee.setName("J K");
        employee.setCompany("Google");
        employee.setSalary(1000);
        
        Employee  employee1 = new Employee();
        employee1.setEmpId(2);
        employee1.setName("J K");
        employee1.setCompany("Google");
        employee1.setSalary(2000);
        
        Employee  employee2 = new Employee();
        employee2.setEmpId(3);
        employee2.setName("J K");
        employee2.setCompany("Google");
        employee2.setSalary(3000);
        
        Employee  employee3 = new Employee();
        employee3.setEmpId(4);
        employee3.setName("SHA");
        employee3.setCompany("APAR");
        employee3.setSalary(4000);
        
        Employee  employee4 = new Employee();
        employee4.setEmpId(5);
        employee4.setName("HM");
        employee4.setCompany("PAYTM");
        employee4.setSalary(5000);
        
        Employee  employee5 = new Employee();
        employee5.setEmpId(5);
        employee5.setName("HM");
        employee5.setCompany("APAR");
        employee5.setSalary(6000);
        
        Employee  employee6 = new Employee();
        employee6.setEmpId(6);
        employee6.setName("JK");
        employee6.setCompany("APAR");
        employee6.setSalary(7000);
        
        Employee  employee7 = new Employee();
        employee7.setEmpId(7);
        employee7.setName("JK");
        employee7.setCompany("JET BRAINS");
        employee7.setSalary(8000);
        
        Employee  employee8 = new Employee();
        employee8.setEmpId(8);
        employee8.setName("AJ");
        employee8.setCompany("APAR");
        employee8.setSalary(9000);
        
        Employee  employee9 = new Employee();
        employee9.setEmpId(8);
        employee9.setName("AJ");
        employee9.setCompany("ADOBE");
        employee9.setSalary(10000);
        
        empList.add(employee);
        empList.add(employee1);
        empList.add(employee2);
        empList.add(employee3);
        empList.add(employee4);
        empList.add(employee5);
        empList.add(employee6);
        empList.add(employee7);
        empList.add(employee8);
        empList.add(employee9);
return empList;
}
}

Output in console .....

Predicate Functional Interface Application ............ 

Employee [empId=5, name=HM, company=APAR, salary=6000]
Employee [empId=6, name=JK, company=APAR, salary=7000]
Employee [empId=7, name=JK, company=JET BRAINS, salary=8000]
Employee [empId=8, name=AJ, company=APAR, salary=9000]
Employee [empId=8, name=AJ, company=ADOBE, salary=10000]

--------- Composing of Predicate  -----------

Employee [empId=1, name=J K, company=Google, salary=1000]
Employee [empId=2, name=J K, company=Google, salary=2000]
Employee [empId=3, name=J K, company=Google, salary=3000]
Employee [empId=4, name=SHA, company=APAR, salary=4000]
Employee [empId=5, name=HM, company=PAYTM, salary=5000]
Employee [empId=5, name=HM, company=APAR, salary=6000]
Employee [empId=6, name=JK, company=APAR, salary=7000]
Employee [empId=7, name=JK, company=JET BRAINS, salary=8000]
--------- Negate and and Predicate -----------
Employee [empId=1, name=J K, company=Google, salary=1000]
Employee [empId=2, name=J K, company=Google, salary=2000]
Employee [empId=3, name=J K, company=Google, salary=3000]

--------- Use of negate ,and ,or  Predicate -----------

Employee [empId=1, name=J K, company=Google, salary=1000]
Employee [empId=2, name=J K, company=Google, salary=2000]
Employee [empId=3, name=J K, company=Google, salary=3000]
Employee [empId=8, name=AJ, company=ADOBE, salary=10000]


No comments:

Post a Comment