Function compose example

Description

Function compose returns a composed function that first applies the before function to its input, and then applies this function to the result.

Syntax

compose has the following syntax.


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

Example

The following example shows how to use compose.


import java.util.function.Function;
// w  ww  . j  av a2s . c  o m
public class Main {

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

The code above generates the following result.