Saturday, 15 August 2015

Java8 Consumer interface

‘java.util.function.Consumer’ is a functional interface. It has functional method ‘accept’, which takes an object as input and perform some operation on it.

You can use this interface when you need to access an object of type T and perform some operations on it.

For example, I want to increment all employee’s salaries like following.

Salary = salary + 0.5 * salary (if salary > 50000)
           = salary + 0.7 * salary (else)

processEmployees(employees, emp -> {
         double salary = emp.getSalary();
         salary = salary > 50000 ? salary + 0.5 * salary : salary + 0.7* salary;
         emp.setSalary(salary);
});

Above snippet calls processEmployees function with list of employees and consumer logic, which increments employee’s salaries as per requirement.
Complete application is given below.

import java.util.Objects;

public class Employee {
  private int id;
  private String firstName;
  private String lastName;
  private int age;
  private String city;
  private double salary;

  public Employee(int id, String firstName, String lastName, int age,
      String city, double salary) {
    super();
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.city = city;
    this.salary = salary;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public String getCity() {
    return city;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public double getSalary() {
    return salary;
  }

  public void setSalary(double salary) {
    this.salary = salary;
  }

  @Override
  public boolean equals(Object employee) {
    if (Objects.isNull(employee))
      return false;

    if (!(employee instanceof Employee))
      return false;

    Employee emp = (Employee) employee;

    return id == emp.id;
  }

  @Override
  public int hashCode() {
    return Objects.hash(id, firstName, lastName, age);
  }

  @Override
  public String toString() {
    return String.format("%s(%s,%d,%f)", firstName, city, age, salary);
  }

}


import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class EmployeeTest {

  public static <T> void processEmployees(List<T> employees,
      Consumer<T> consumer) {
    for (T emp : employees) {
      consumer.accept(emp);
    }
  }

  public static void main(String args[]) {
    Employee emp1 = new Employee(1, "Hari Krishna", "Gurram", 26,
        "Bangalore", 45000);
    Employee emp2 = new Employee(2, "Joel", "Chelli", 27, "Hyderabad",
        40000);
    Employee emp3 = new Employee(3, "Shanmukh", "Kummary", 28, "Chennai",
        50000);
    Employee emp4 = new Employee(4, "Harika", "Raghuram", 27, "Chennai",
        70000);
    Employee emp5 = new Employee(5, "Sudheer", "Ganji", 27, "Bangalore",
        55000);
    Employee emp6 = new Employee(6, "Rama Krishna", "Gurram", 27,
        "Bangalore", 100000);
    Employee emp7 = new Employee(7, "PTR", "PTR", 27, "Hyderabad", 150000);
    Employee emp8 = new Employee(8, "Siva krishna", "Ponnam", 28,
        "Hyderabad", 65000);

    List<Employee> employees = new ArrayList<>();

    employees.add(emp1);
    employees.add(emp2);
    employees.add(emp3);
    employees.add(emp4);
    employees.add(emp5);
    employees.add(emp6);
    employees.add(emp7);
    employees.add(emp8);

    System.out.println("Employee salaries before incrementing");
    System.out.println(employees);

    processEmployees(employees, emp -> {
      double salary = emp.getSalary();
      salary = salary > 50000 ? salary + 0.5 * salary : salary + 0.7
          * salary;
      emp.setSalary(salary);
    });

    System.out.println("\nEmployee salaries after incrementing");
    System.out.println(employees);
  }
}


Output

Employee salaries before incrementing
[Hari Krishna(Bangalore,26,45000.000000), Joel(Hyderabad,27,40000.000000), Shanmukh(Chennai,28,50000.000000), Harika(Chennai,27,70000.000000), Sudheer(Bangalore,27,55000.000000), Rama Krishna(Bangalore,27,100000.000000), PTR(Hyderabad,27,150000.000000), Siva krishna(Hyderabad,28,65000.000000)]

Employee salaries after incrementing
[Hari Krishna(Bangalore,26,76500.000000), Joel(Hyderabad,27,68000.000000), Shanmukh(Chennai,28,85000.000000), Harika(Chennai,27,105000.000000), Sudheer(Bangalore,27,82500.000000), Rama Krishna(Bangalore,27,150000.000000), PTR(Hyderabad,27,225000.000000), Siva krishna(Hyderabad,28,97500.000000)]



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment