What is the Function interface in Java 8, and how is it used?

Function<T, R> is a functional interface in java.util.function representing a transformation that takes one argument of type T and produces a result of type R, defined by its single abstract method apply(T t).

Key Points

Example

Function<String, Integer> parseLength = String::length; can be passed to stream.map(parseLength) to turn a stream of strings into a stream of their lengths.

Function<Integer, Integer> square = x -> x * x;
Function<Integer, Integer> addOne = x -> x + 1;

Function<Integer, Integer> combined = square.andThen(addOne);
System.out.println(combined.apply(3)); // 10 -> (3*3)+1

Interview Tip

A concise interview answer is

Function<T, R> represents a transformation from one type to another through its apply() method, and it's the interface Stream.map() expects. Its andThen() and compose() default methods let me chain multiple functions into a pipeline, which is handy for building reusable transformation steps.

Last updated: 16 July 2026

Previous question: How do you create an infinite stream in Java 8?

Next question: What are method references in Java 8, and how do they relate to Lambda Expressions?

More Java 8 interview questions

All Java 8 questions