Home  >  JDK 8

Function.apply in Java 8

By Arvind Rai, 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)
Where T is the function argument and R is the result.
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);
    }
}
There is a customShow() method which is accepting Function interface. Now we can create function instance in three ways.
for(Student st: list){
    System.out.println(st.customShow(s->s.name+": "+s.name));
}
This is first way. Just pass the s->s.name+": "+s.name as Function instance to customShow method.
Function<Student,String> styleOne = s->{
     String result =  "Name:"+s.name +" and Age:"+s.age;
     return result;
};
In second way, we declare Function in separate place and manipulate the result and finally return.
Function<Student,String> styleTwo = s->        
            "Name:"+s.name +" and Age:"+s.age;
In third way, define function in a single line.
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));
        }
        
    }
} 
Output will be as below.
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
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS








Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us