Java8 - java.util.function.Function Interface tutorial with Examples



This is a simple Functional interface, with which method takes as input and return the output. This interface is used to mapping the objects - which takes one type as input, process it and returns the output in another type. This interface has an only single abstract method.java.util.function.The function is a predefined interface introduced in java 8.

This post is about the predefined functional interface - Function examples with tutorials. And also explained about  usage of apply(), andThen(), componse() method with examples

public interface Function {  
   public R apply(T t);  
}  

T - input value as given R - output or return value.
 Here object type T is input to a lambda expression, process and Object T is return as the value.

Function methods 

Default methods are andThen() and compose() methods, Static methods are identity() method

java.util.function.Function methods

Function interface apply method example

This function apply method takes the input is string and output is a string. apply() method process string and return the string as the value

import java.util.function.Function;  
  
public class MyFunctionExample {  
 static String HelloWorld(String name) {  
  return "Hello " + name;  
 }  
  
 public static void main(String[] args) {  
  Function<String, String> functionalVariable = MyFunctionInterface::HelloWorld;  
  System.out.println(functionalVariable.apply("Frank"));  
 }  
}  

and output is

Hello Frank  

and one more example of apply() method example

import java.util.function.Function;  
  
public class MyFunctionExample {  
  
 public static void main(String[] args) {  
  Function<Integer, Integer> IncrementFunction = (value) -> (value + 1);  
  System.out.println(IncrementFunction.apply(2));  
  System.out.println(IncrementFunction.apply(5));  
  
 }  
  
}  

and output of the above code is

output is 3,6  

Function interface andThen method example

andThen() is default method. This combines the called function with other function after function is applied.

Syntax

default <V> Function<T,V> andThen(Function<? super R,? extends V> after)  

parameter - after the function

import java.util.function.Function;  
public class MyFunctionExample {  
 public static void main(String[] args) {  
  Function<Integer, Integer> IncrementFunction = (value) -> (value + 1);  
     Function<Integer, Integer> doubleFunction = value -> 2*value;  
        double output=IncrementFunction.andThen(doubleFunction).apply(10);  
        System.out.println("output of andThen: "output);  
        }  
}  

The above code Output is

output of andThen: 22.0  

Function interface compose method example

This is the default method. This combines the called function with other function before the function is applied. Syntax

  
default <V> Function<V,R> compose(Function<? super V,? extends T> before)  

import java.util.function.Function;  
public class MyFunctionExample {  
 public static void main(String[] args) {  
  Function<Integer, Integer> IncrementFunction = (value) -> (value + 1);  
     Function<Integer, Integer> doubleFunction = value -> 2*value;  
        double output=IncrementFunction.compose(doubleFunction).apply(10);  
        System.out.println("output of compose: "+output);  
 }  
}  

Output of the above code is

output of compose: 21.0  

Similar Posts