Monday, 5 October 2020

How to Use Function Functional Interface in Java

Function Functional Interface :- Function Functional Interface define one abstract method named apply.
  
public interface Function<T,R>{
  public R apply(T object);
 }
 
 Function Functional Interface apply method takes an object of T type argument and map it to another object of R type then return R type object .

Function Functional Interface Application 
FunctionFunctionalInterface.java
package in.jk.java8.function;

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

public class FunctionFunctionalInterface {

public static void main(String[] args) {
System.out.println("Function Functional Interface Application ...");

List<Employee> employeeList = Employee.getEmployeeList();
System.out.println("\n------- Use of Function  ----------\n");


Function<Employee, EmployeeRequest> function = (employee) -> {
                        EmployeeRequest employeeRequest = new EmployeeRequest();
employeeRequest.setEmpId(employee.getEmpId());
employeeRequest.setName(employee.getName());
employeeRequest.setSalary(employee.getSalary());
employeeRequest.setCompany(employee.getCompany());
return employeeRequest;

};

employeeList.forEach(employee -> {
EmployeeRequest employeeRequest = function.apply(employee);
System.out.println(employeeRequest);
});

System.out.println("\n------- Function Implementation by Class ----------\n");
Function<Employee, EmployeeRequest> function1 = new EmployeeConvertor();

employeeList.forEach(employee -> {
EmployeeRequest employeeRequest = function1.apply(employee);
System.out.println(employeeRequest);

});

System.out.println(" \n Composing of Function  ");
// Composing of Function
Function<Integer, Integer> addFunction = x -> x + 1;
Function<Integer, Integer> multiplyFunction = x -> x * 2;

// Composing of andThen Function
                Function<Integer, Integer> addAndMultiplyFunction=null;
addAndMultiplyFunction =  addFunction.andThen(multiplyFunction);
int result = addAndMultiplyFunction.apply(1);
System.out.println("Composing Result by andThen  ::  " + result);

// Composing of compose Function
                Function<Integer, Integer> addAndMultiplyComposeFunction=null;
addAndMultiplyComposeFunction = addFunction.compose(multiplyFunction);
int result1 = addAndMultiplyComposeFunction.apply(1);
               System.out.println("Composing Result by compose :: " + result1);

}

}

EmployeeConvertor.java

package in.jk.java8.function;
import java.util.function.Function;
import in.jk.java8.Employee;

public class EmployeeConvertor implements Function<Employee, EmployeeRequest> {

@Override
public EmployeeRequest apply(Employee employee) {

EmployeeRequest employeeRequest = new EmployeeRequest();
employeeRequest.setEmpId(employee.getEmpId());
employeeRequest.setName(employee.getName());
employeeRequest.setSalary(employee.getSalary());
employeeRequest.setCompany(employee.getCompany());
                return employeeRequest;
}

}


EmployeeRequest.java
package in.jk.java8.function;

public class EmployeeRequest {
private int empId;
private String name;
private String company;
private int salary;
      //Getters and Setters
@Override
public String toString() {
return "EmployeeRequest [empId=" + empId + ", name=" + name + ", company=" +                               company + ", salary=" + salary
+ "]";
}
}
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 ...
Function Functional Interface Application ...

------- Use of Function  ----------

EmployeeRequest [empId=1, name=J K, company=Google, salary=1000]
EmployeeRequest [empId=2, name=J K, company=Google, salary=2000]
EmployeeRequest [empId=3, name=J K, company=Google, salary=3000]
EmployeeRequest [empId=4, name=SHA, company=APAR, salary=4000]
EmployeeRequest [empId=5, name=HM, company=PAYTM, salary=5000]
EmployeeRequest [empId=5, name=HM, company=APAR, salary=6000]
EmployeeRequest [empId=6, name=JK, company=APAR, salary=7000]
EmployeeRequest [empId=7, name=JK, company=JET BRAINS, salary=8000]
EmployeeRequest [empId=8, name=AJ, company=APAR, salary=9000]
EmployeeRequest [empId=8, name=AJ, company=ADOBE, salary=10000]

------- Function Implementation by Class ----------

EmployeeRequest [empId=1, name=J K, company=Google, salary=1000]
EmployeeRequest [empId=2, name=J K, company=Google, salary=2000]
EmployeeRequest [empId=3, name=J K, company=Google, salary=3000]
EmployeeRequest [empId=4, name=SHA, company=APAR, salary=4000]
EmployeeRequest [empId=5, name=HM, company=PAYTM, salary=5000]
EmployeeRequest [empId=5, name=HM, company=APAR, salary=6000]
EmployeeRequest [empId=6, name=JK, company=APAR, salary=7000]
EmployeeRequest [empId=7, name=JK, company=JET BRAINS, salary=8000]
EmployeeRequest [empId=8, name=AJ, company=APAR, salary=9000]
EmployeeRequest [empId=8, name=AJ, company=ADOBE, salary=10000]
 
 Composing of Function  
Composing Result by andThen  ::  4
Composing Result by compose :: 3



No comments:

Post a Comment