Java Lambda – Function apply example

By | September 18, 2017

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

--------------------------------------------------------------

Interface Function<T,R>

  • Type Parameters:
    T – the type of the input to the function
    R – the type of the result of the function
    All Known Subinterfaces:
    UnaryOperator<T>
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface Function<T,R>
    Represents a function that accepts one argument and produces a result.This is a functional interface whose functional method is apply(Object).

    Since:
    1.8

—————————————————————————————-

Function apply applies this function to the given argument.

Syntax

apply has the following syntax.

R apply(T t)

Example

The following example shows how to use apply.

import java.util.function.Function;
//from ww w  .  ja  v a 2 s.c  om
public class Main {

  public static void main(String[] args) {
    Function<Integer,String> converter = (i)-> Integer.toString(i);
    
   
    System.out.println(converter.apply(3).length());
    System.out.println(converter.apply(30).length());
  }
}

The code above generates the following result.

1

2

About Functions that Create Functions

Let’s see a couple of examples of how we can do this using function objects. Let’s consider the following example:

Function<Integer, Function<Integer,Integer>> makeAdder = x -> y -> x + y;

Above we have a function called makeAdder that takes an integer x and creates a new function that takes an integer y and when the latter is invoked, it adds xto y. We can tell this is a high order function because it produces a new function.

Now, we use this to create a new version of the add1 function:

Function<Integer,Integer> add1 = makeAdder.apply(1);
Function<Integer,Integer> add2 = makeAdder.apply(2);
Function<Integer,Integer> add3 = makeAdder.apply(3);