Saturday 15 August 2015

Function interface

A Functional interface is the interface, which contains only one abstract method. Java 8 uses functional interfaces while implementing lambda expressions.

Some of the examples of functional interfaces
java.util.function.Predicate
java.util.function.Function
java.lang.Runnable

I Would recommend you go through my below tutorials to understand lambda expressions.



java.util.function.Function interface
‘java.util.function.Function’ is a functional interface contains method ‘apply’. Apply function accepts one argument and produces a result.

As you see the above image, ‘java.util.function.Function’ interface has below 3 things to note.
1.   @FunctionalInterface annotation is used to tell jvm, this is a functional interface
2.   R apply(T t) : It is an abstract method

3.   Remaining all methods are concrete.

For example, following application gets employee id and salaries by using Function interface.

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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class EmployeeTest<R> {

 public static <T, R1, R2> Map<R1, R2> getEmpDetails(List<T> employees,
   Function<T, R1> function1, Function<T, R2> function2) {
  Map<R1, R2> tmp = new HashMap<>();
  for (T emp : employees) {
   R1 id = function1.apply(emp);
   R2 name = function2.apply(emp);
   tmp.put(id, name);
  }
  return tmp;
 }

 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);

  Map<Integer, String> details = getEmpDetails(employees,
    (emp) -> emp.getId(), (emp) -> emp.getFirstName());
  System.out.println(details);

 }
}


Output
{1=Hari Krishna, 2=Joel, 3=Shanmukh, 4=Harika, 5=Sudheer, 6=Rama Krishna, 7=PTR, 8=Siva krishna}


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment