Function.apply in Java 8
April 06, 2014
java.util.function.Function is an interface and has been introduced in java 8. Function is a functional interface. So it can be used to accept lambda expression. Function accepts one argument and returns the result. Function interface contains one method that is apply(). This is the functional interface method. Find the declaration of apply() method.
R apply(T t)
To use it we need to define Function. Suppose we have a method customShow () inside student class which will accept Function instance. Find the student class.
Student.java
package com.concretepage.lambda; import java.util.function.Function; public class Student { public String name; public int age; public Student(String name,int age){ this.name = name; this.age = age; } public String customShow(Function<Student,String> fun){ return fun.apply(this); } }
for(Student st: list){ System.out.println(st.customShow(s->s.name+": "+s.name)); }
Function<Student,String> styleOne = s->{ String result = "Name:"+s.name +" and Age:"+s.age; return result; };
Function<Student,String> styleTwo = s-> "Name:"+s.name +" and Age:"+s.age;
So customShow() method will accept the Function instance and the apply() method of Function will execute how the function is defined. Find the complete example.
FunctionDemo.java
package com.concretepage.lambda; import java.util.ArrayList; import java.util.List; import java.util.function.Function; public class FunctionDemo { public static void main(String[] args) { List<Student> list = new ArrayList(); list.add(new Student("Ram",20)); list.add(new Student("Shyam",22)); list.add(new Student("Kabir",18)); // Simple use of function for(Student st: list){ System.out.println(st.customShow(s->s.name+": "+s.name)); } //Style one to declare function Function<Student,String> styleOne = s->{ String result = "Name:"+s.name +" and Age:"+s.age; return result; }; //Style two to declare function Function<Student,String> styleTwo = s-> "Name:"+s.name +" and Age:"+s.age; System.out.println("--print value by style one--"); //print the values of list using stle one function for(Student st: list){ System.out.println(st.customShow(styleOne)); } System.out.println("--print value by style two--"); //print the values of list using style two function for(Student st: list){ System.out.println(st.customShow(styleTwo)); } } }
Ram: Ram Shyam: Shyam Kabir: Kabir --print value by style one-- Name:Ram and Age:20 Name:Shyam and Age:22 Name:Kabir and Age:18 --print value by style two-- Name:Ram and Age:20 Name:Shyam and Age:22 Name:Kabir and Age:18