Combining two or more Function functional interface

In this post, I will explain how to combine Function functional interfaces.

They are two ways in which we can combine Function functional interface and it is achieved using two methods in Function interface as mentioned below
1) default Function andThen(Function after)
2) default Function compose(Function before)

Both the methods accept an implementation of Function interface as an argument and return another implementation of Function interface.

The difference between the two methods is, say they are two Function functional interface implementations named function1 and function2.

The below code will return a new implementation of Function interface composing function1 and function2, with function1 called first with the given input and the result of function1 is passed as input to function2.


    function1.andThen(function2);

The below code will return a new implementation of Function interface composing function1 and function2, with function2 called first with the given input and then result of function2 is passed as input to function1.


    function1.compose(function2);

For our example we create four implementations of Function interface as shown below


class CustomFunction1 implements Function {
    @Override
    public Integer apply(String data) {
        System.out.println("CustomFunction1");
        Integer value = Integer.parseInt(data);
        return value;
    }
}

class CustomFunction2 implements Function {
    @Override
    public Integer apply(Integer value) {
        System.out.println("CustomFunction2");
        return value * 2;
    }
}

class CustomFunction3 implements Function {
    @Override
    public Float apply(String data) {
        System.out.println("CustomFunction3");
        Float value = Float.parseFloat(data);
        return value;
    }
}

class CustomFunction4 implements Function {
    @Override
    public Float apply(Float value) {
        System.out.println("CustomFunction4");
        return value * 2;
    }
}

CustomFunction1 takes String argument and returns the integer version.
CustomFunction2 takes integer argument and multiplies it with 2.
CustomFunction3 takes String argument and returns the float version.
CustomFunction4 takes float argument and multiplies it with 2.

For our example we combine CustomFunction1 and CustomFunction2 and return a new implementation of Function functional interface. We also combine CustomFunction3 and CustomFunction4 and return a new implementation of Function functional interface.

Below is the complete main code

Main Code


1  package function;
2  import java.util.function.Function;
3  
4  public class FunctionDemo2 {
5   public static void main(String[] args) {
6       CustomFunction1 customFunction1 = new CustomFunction1();
7       CustomFunction2 customFunction2 = new CustomFunction2();
8       CustomFunction3 customFunction3 = new CustomFunction3();
9       CustomFunction4 customFunction4 = new CustomFunction4();
10      
11      Function customFunctionStringToInteger = customFunction2.compose(customFunction1);
12      
13      Integer resultStringToInteger = customFunctionStringToInteger.apply("2");
14      System.out.println(resultStringToInteger);
15      
16      Function customFunctionStringToFloat = customFunction3.andThen(customFunction4);
17      Float resultStringToFloat = customFunctionStringToFloat.apply("3");
18      System.out.println(resultStringToFloat);
19  }
20 }

At line 11, we combine CustomFunction1 and CustomFunction2 using compose method. So CustomFunction1 is called first and the result of the method will be passed to CustomFunction2.

At line 16, we combine CustomFunction3 and CustomFunction4 using andThen method. So CustomFunction3 is called first and the result of the method will be passed to CustomFunction4.

Next we execute the new composed functional interface by calling apply method. Refer to line 13 and 17.

Output

CustomFunction1
CustomFunction2
4
CustomFunction3
CustomFunction4
6.0


Check these products on Amazon

Leave a Reply