What is a functional interface in Java?

Miranda 168 Published: 07/15/2024

What is a functional interface in Java?

I'm happy to explain the concept of a functional interface in Java!

In Java, a functional interface is a special type of interface that can be used as a lambda expression or method reference. This means you can pass instances of these interfaces as arguments to various higher-order functions, such as Collections.sort() or Predicate.test().

A functional interface is defined by the Java Language Specification (JLS) as an interface that has exactly one abstract method. Yes, just one! This single abstract method (SAM) is often referred to as the "function" of the interface, which explains why these interfaces are called "functional."

Here's a simple example of a functional interface in Java:

@FunctionalInterface

interface MyFunction {

int apply(int x);

}

As you can see, the MyFunction interface has only one abstract method, apply(), which takes an int parameter and returns an int. This makes it a valid functional interface.

Now, let's talk about how you can use these interfaces. Suppose we want to filter out even numbers from an array of integers:

List numbers = Arrays.asList(1, 2, 3, 4, 5);

// Create a lambda expression that represents the filter function

Predicate evenFilter = x -> x % 2 == 0;

// Use the predicate to filter out even numbers

List filteredNumbers = numbers.stream()

.filter(evenFilter)

.collect(Collectors.toList());

System.out.println(filteredNumbers); // [1, 3, 5]

In this example, we define a functional interface Predicate (which is actually a built-in Java interface) and create a lambda expression that represents the filter function. We then use this predicate to filter out even numbers from the original list.

The beauty of functional interfaces lies in their versatility. You can use them as lambda expressions or method references, making your code more concise and expressive. For instance:

// Using a lambda expression

List numbers = Arrays.asList(1, 2, 3, 4, 5);

numbers.stream()

.filter(x -> x % 2 == 0) // equivalent to evenFilter above

.collect(Collectors.toList());

Or, you can use a method reference:

// Using a method reference

List numbers = Arrays.asList(1, 2, 3, 4, 5);

numbers.stream()

.filter(Math::remainder % 2 == 0) // equivalent to evenFilter above

.collect(Collectors.toList());

As you can see, functional interfaces are a powerful tool in Java, allowing you to write more concise and expressive code. They're also a fundamental building block of the Java Stream API.

In summary, a functional interface in Java is an interface that has exactly one abstract method (SAM), which makes it possible to use as a lambda expression or method reference. This enables you to write more concise and expressive code, leveraging the power of higher-order functions and the Java Stream API.

functional interface in java 8

I see you're asking about functional interfaces in Java 8!

In Java 8, a functional interface is an interface that has exactly one abstract method (also known as the "target method"). This concept was introduced to support lambda expressions and method references.

Here are some key characteristics of functional interfaces:

Exactly one abstract method: A functional interface must have only one abstract method. If it has more than one, it's not a functional interface. No default methods: Functional interfaces cannot have any default methods (also known as "default" or "static" methods). No static methods: Like with default methods, functional interfaces cannot have any static methods.

When you create an instance of a functional interface using lambda expression, it creates an implementation for the target method.

Here's an example of a simple functional interface:

@FunctionalInterface

interface Printer {

void print(String text);

}

Now, let's see how we can use this interface to create a printer that prints messages:

Printer printer = (text) -> System.out.println(text);

printer.print("Hello, World!");

When you run this code, it will print "Hello, World!".

Functional interfaces are useful when you need to perform some operation on a single method. This concept is widely used in Java 8 with lambda expressions and method references, which I'll discuss further below.

Lambda Expressions

Lambda expressions are anonymous functions that can be defined inline within your code. They're a concise way to implement functional interfaces.

Here's an example of using a lambda expression with our Printer interface:

Printer printer = text -> System.out.println(text);

printer.print("Hello, World!");

When you run this code, it will print "Hello, World!" again!

Method References

Method references are a shorthand way to create lambda expressions that refer to existing methods.

Here's an example of using method reference with our Printer interface:

System.out::println

You can use this method reference as follows:

Printer printer = System.out::println;

printer.print("Hello, World!");

Again, when you run this code, it will print "Hello, World!".

In summary, functional interfaces are a fundamental concept in Java 8 that support lambda expressions and method references. They're useful for implementing single-method interfaces that can be used with lambda expressions or method references.