Function Functional Interface

Function Functional Interface
  • Functions are exactly same as predicates except that functions can return any type of result but function should (can) return only one value and that value can be any type as per our requirement.
  • To implement functions oracle people introduced Function interface in 1.8version.
  • Function interface present in Java.util.function package.
  • Functional interface contains only one method i.e., apply().
  • Function is a functional interface and hence it can refer lambda expression.
interface Function(T,R) {
   public R apply(T t);
}

Function to find length of given input string.

package com.ashok.java8.function;

import java.util.function.Function;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class StringLengthFunction {
   public static void main(String[] args) {
      Function<String, Integer> f = s -> s.length();
      System.out.println(f.apply("Ashok Kumar")); // 11
   }
}
Differences between predicate and function
Capture 46

Note

Predicate is a boolean valued function and(), or(), negate() are default methods present inside Predicate interface.

2. Remove spaces present in the given String
package com.ashok.java8.function;

import java.util.function.Function;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class RemoveSpaces {
   public static void main(String[] args) {
      String str = "Hi this is Ashok Kumar";
      FunctionFunction<String, String> f = s -> s.replaceAll(" ", "");
      System.out.println(f.apply(str)); // HithisisAshokKumar
   }
}
3. Find Number of spaces present in the given String
package com.ashok.java8.function;

import java.util.function.Function;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class NumberOfSpaces {
   public static void main(String[] args) {
      String str = "Hi this is Ashok Kumar";
      Function<String, Integer> f = s -> s.length() - s.replaceAll(" ","").length();
      System.out.println(f.apply(str)); // 4
   }
}
Function Chaining

We can combine multiple functions together to form more complex functions. For this Function interface defines the following 2 default methods:

f1.andThen(f2)

First f1 will be applied and then for the result f2 will be applied.

f1.compose(f2)

First f2 will be applied and then for the result f1 will be applied.

package com.ashok.java8.function;

import java.util.function.Function;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class FunctionChaining {
   public static void main(String[] args) {
      Function<String, String> f1 = s -> s.toUpperCase();
      Function<String, String> f2 = s -> s.substring(0, 5);

      System.out.println("The Result of f1 : " + f1.apply("Ashok Kumar"));
      System.out.println("The Result of f2 : " + f2.apply("Ashok Kumar"));
      System.out.println("The Result of f1.andThen(f2) : " + f1.andThen(f2).apply("Ashok Kumar"));
      System.out.println("The Result of f1.compose(f2) : " + f1.compose(f2).apply("Ashok Kumar"));
   }
}
package com.ashok.java8.function;

import java.util.function.Function;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class FunctionChainingTest {
   public static void main(String[] args) {
      Function<Integer, Integer> f1 = i -> i + i;
      Function<Integer, Integer> f2 = i -> i * i * i;
      
      System.out.println(f1.andThen(f2).apply(2)); // 64
      System.out.println(f1.compose(f2).apply(2)); // 16
   }
}
Function interface Static Method : identity()

Function interface contains a static method.

static <T> Function<T,T> identity()

Returns a function that always returns its input argument.

package com.ashok.java8.function;

import java.util.function.Function;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class IdentityTest {
   public static void main(String[] args) {
      Function<String,String> f1= Function.identity();
      System.out.println(f1.apply("Ashok Kumar")); // Ashok Kumar
   }
}
Function Functional Interface
Scroll to top