Consumer Functional Interface :- Consumer Functional Interface have one abstract method name with accept . Consumer Functional Interface accept method takes an object as argument and perform some operation on it .
public interface Consumer<T>{
public void accept(T object);
}
Consumer Functional Interface Application
ConsumerFunctionalInterfaceApplication .java
package in.jk.java8.consumer;
import java.util.List;
import java.util.function.Consumer;
import in.jk.java8.Employee;
public class ConsumerFunctionalInterfaceApplication {
public static void main(String[] args) {
System.out.println("Consumer Functional Interface Application .... ");
List<Employee> employeeList = Employee.getEmployeeList();
System.out.println("\n--------------- Salary Calculator Consumer -------------------\n");
Consumer<Employee> consumer = (employee) -> {
int salary = employee.getSalary();
int totalSalary = salary + 1000;
employee.setSalary(totalSalary);
};
employeeList.forEach(employee -> {
consumer.accept(employee);
System.out.println(employee);
});
System.out.println("\n------ Salary Calculator Consumer Implementation By Class --------\n");
Consumer<Employee> SalaryCalculatorConsumer = new SalaryCalculator();
employeeList.forEach(employee -> {
SalaryCalculatorConsumer.accept(employee);
System.out.println(employee);
});
}
}
SalaryCalculator.java
package in.jk.java8.consumer;
import java.util.function.Consumer;
import in.jk.java8.Employee;
public class SalaryCalculator implements Consumer<Employee> {
@Override
public void accept(Employee employee) {
int salary = employee.getSalary();
int totalSalary = salary + 1000;
employee.setSalary(totalSalary);
}
}
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 .....
Consumer Functional Interface Application ....
--------------- Salary Calculator Consumer -------------------
Employee [empId=1, name=J K, company=Google, salary=2000]
Employee [empId=2, name=J K, company=Google, salary=3000]
Employee [empId=3, name=J K, company=Google, salary=4000]
Employee [empId=4, name=SHA, company=APAR, salary=5000]
Employee [empId=5, name=HM, company=PAYTM, salary=6000]
Employee [empId=5, name=HM, company=APAR, salary=7000]
Employee [empId=6, name=JK, company=APAR, salary=8000]
Employee [empId=7, name=JK, company=JET BRAINS, salary=9000]
Employee [empId=8, name=AJ, company=APAR, salary=10000]
Employee [empId=8, name=AJ, company=ADOBE, salary=11000]
--------------- Salary Calculator Consumer Implemetation By Class -------------------
Employee [empId=1, name=J K, company=Google, salary=3000]
Employee [empId=2, name=J K, company=Google, salary=4000]
Employee [empId=3, name=J K, company=Google, salary=5000]
Employee [empId=4, name=SHA, company=APAR, salary=6000]
Employee [empId=5, name=HM, company=PAYTM, salary=7000]
Employee [empId=5, name=HM, company=APAR, salary=8000]
Employee [empId=6, name=JK, company=APAR, salary=9000]
Employee [empId=7, name=JK, company=JET BRAINS, salary=10000]
Employee [empId=8, name=AJ, company=APAR, salary=11000]
Employee [empId=8, name=AJ, company=ADOBE, salary=12000]
No comments:
Post a Comment