Java 8 Function Interface Example

In this tutorial, we will learn how to use Function functional interface with an example.
The Function is a functional interface introduced in Java 8; it takes an argument (object of type T) and returns an object (object of type R). The argument and output can be a different type.
check out functional interfaces tutorial at https://www.javaguides.net/2018/07/java-8-functional-interfaces.html
Check out Java 8 tutorial at https://www.javaguides.net/p/java-8.html

Video Tutorial

Function interface source code

java.util.function.Function is a functional interface whose functional method (single abstract method) is R apply(T t). The Function interface represents an operation that takes single argument T and returns a result R.
This is the internal implementation of the Function interface:
@FunctionalInterface
public interface Function<T, R> {

      R apply(T t);

}
  • T – Type of the input to the function.
  • R – Type of the result of the function.

Check out Javadoc of Function interface at https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html

Function interface example #1

This example takes a String and returns the length of the string as Integer.
package com.javaguides.java.functionalinterfaces;

import java.util.function.Function;

public class FunctionDemo {
    public static void main(String[] args) {

        // example 1
        Function < String, Integer > function = (t) -> t.length();
        System.out.println(function.apply("Ramesh"));
    }
}
Output:
6

Function interface example #2

package com.javaguides.java.functionalinterfaces;

import java.util.function.Function;

public class FunctionDemo {
    public static void main(String[] args) {
        // example 2
        Function < Integer, String > function2 = (number) -> {
            if (number % 2 == 0) {
                return "Number " + number + " is even";
            } else {
                return "Number " + number + " is odd";
            }
        };

        System.out.println(function2.apply(11));
    }
}
Output:
Number 11 is odd

Chain Function<T, R> example #3

This example chains the Function with andThen() method:
package com.javaguides.java.functionalinterfaces;

import java.util.function.Function;

public class FunctionDemo {
    public static void main(String[] args) {

        // example 3
        Function < String, Integer > function3 = (t) -> t.length();
        Function < Integer, Integer > function4 = (number) -> number * 2;

        Integer integer = function3.andThen(function4).apply("Ramesh");
        System.out.println(integer);
    }
}
Output:
12

Convert List to Map example #4

This example accepts Function as an argument, convert a List into a Map
package com.javaguides.java.functionalinterfaces;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class FunctionDemo {
    public static void main(String[] args) {
         // example 4
        List<String> list = Arrays.asList("Banana", "Mango", "Apple", "Watermelon");
        // lambda
        Map<String, Integer> map = convertListToMap(list, x -> x.length());

        System.out.println(map); // {node=4, c++=3, java=4, javascript=10}
    }

    private static <T, R> Map<T, R> convertListToMap(List<T> list, Function<T, R> func) {

        Map<T, R> result = new HashMap<>();
        for (T t : list) {
            result.put(t, func.apply(t));
        }
        return result;
    }
}
Output:
{Apple=5, Watermelon=10, Mango=5, Banana=6}

check out functional interfaces tutorial at https://www.javaguides.net/2018/07/java-8-functional-interfaces.html
Check out Java 8 tutorial at https://www.javaguides.net/p/java-8.html

Comments