Functional Interface in Java 8


Java 8 introduced a significant enhancement known as lambda expressions, which allowed for more concise and expressive code. Alongside lambda expressions, Java 8 also introduced a concept called “Functional Interface”. In this blog post, we will explore what is functional interface, provide examples of how to use them, and list some of the commonly used functional interfaces in Java 8.

1. What is a Functional Interface?

In Java, a functional interface is an interface that has only one abstract method. These interfaces are also referred to as “SAM” (Single Abstract Method) interfaces. The introduction of functional interfaces was pivotal in Java’s evolution as it enabled the use of lambda expressions to implement these interfaces concisely.

The key idea behind functional interfaces is to simplify the use of lambdas for method implementations, thus promoting clean and readable code. Java 8 provides a range of predefined functional interfaces for common use cases, making it easy to leverage lambda expressions in your code.

2. Using Functional Interface

To understand functional interfaces better, let’s consider a simple example where we create a functional interface and use it with a lambda expression:

Test.java
package org.websparrow;


@FunctionalInterface
interface MyFunctionalInterface {
    void doSomething(String message);
}

public class Test {

    public static void main(String[] args) {

        MyFunctionalInterface myLambda = (message) -> {
            System.out.println("Doing something with the message: " + message);
        };

        myLambda.doSomething("Hello, Java 8!");
    }
}

In this example, MyFunctionalInterface is a functional interface with a single abstract method doSomething. We create an instance of this functional interface using a lambda expression and call the doSomething method.

Output:

console.log
Doing something with the message: Hello, Java 8!

Related Posts:

  1. Java 8 Consumer and Supplier Example
  2. Java 8 Predicate vs Function: A Comparison with Examples
  3. Lambda Expressions in Java 8

3. Commonly used Functional Interfaces in Java 8

Java 8 provides a set of commonly used functional interfaces in the java.util.function package, which can simplify your coding efforts significantly. Here’s a list of some of these interfaces:

  1. Consumer<T>: Represents an operation that accepts a single argument and returns no result. It is used for side-effect operations.
  2. Supplier<T>: Represents a supplier of results. It has no arguments and provides a value.
  3. Predicate<T>: Represents a boolean-valued function that accepts one argument and returns a boolean result.
  4. Function<T, R>: Represents a function that accepts an argument of type T and produces a result of type R.
  5. UnaryOperator<T>: Represents a function that accepts an argument of type T and produces a result of type T.
  6. BinaryOperator<T>: Represents a function that accepts two arguments of type T and produces a result of type T.

These interfaces cover a wide range of use cases, making your code more modular and expressive. They are commonly used with lambda expressions to streamline various programming tasks.

4. Summary

Functional interfaces, alongside lambda expressions, are powerful additions to Java’s toolbox, allowing for cleaner and more expressive code. Additionally, Java 8’s predefined functional interfaces in the java.util.function package simplify your development efforts, reducing the need for custom interface declarations.

5. References

  1. Package java.util.function – JavaDoc
  2. Overview of Java 8 Date and Time API
  3. Exploring the Optional in Java 8
  4. Lambda Expressions in Java 8

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.