Skip to content

What Is a Function Interface In Java?

This lesson talks about the third functional interface, which is the Function.

Gopi Gorantala
Gopi Gorantala
1 min read

Table of Contents

What is a Function interface?

The function interface is a function that maps a value to a different value. Accepts one argument and produces a result.

The Function interface is a functional interface defined in the java.util.function package. It represents a function that accepts an input of one type and produces a result of another type.

The Function interface is widely used in functional programming scenarios where you need to transform or convert data from one form to another.

The Function interface declares a single abstract method called apply(), which takes an argument of the input type and returns a value of the result type. Here's the method signature of the apply() method:

Syntax

R apply(T t);

The type parameters T and R represent the input type and result type, respectively. They can be any valid Java type, including primitive types and objects.

To use the Function interface, you can create an instance of it using a lambda expression or method reference that implements the apply() method. Here's an example that demonstrates how to use a Function to convert a string to its length:

import java.util.function.Function;

public class FunctionExample {

  public static void main(String[] args) {
    Function<String, Integer> stringLengthFunction = str -> str.length();

    // Calling the apply() method to get the length of a string
    int length = stringLengthFunction.apply("Hello, World!");
    System.out.println("Length: " + length); // Length: 13
  }
}

In the example, we create a Function<String, Integer> using a lambda expression that takes a string as input and returns its length using the length() method of the String class. Then, we call the apply() method on the Function instance to obtain the length of a given string.

The Function interface is commonly used in functional programming and enables a wide range of transformations and mappings on data. It can be used to transform one type to another, apply calculations or algorithms, or perform any custom processing that involves input and output data.

Note that the Function interface is part of the Java 8 functional interfaces introduced to support lambda expressions and functional programming constructs. It is often used in combination with other functional interfaces like Consumer, Supplier, and Predicate to compose complex functional pipelines and perform data manipulation in a concise and expressive manner.

Java Streams APIJava

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class.