Home Java 8 - Functional Interfaces
Post
Cancel

Java 8 - Functional Interfaces

Java 8 - Functional Interfaces

Functional Interfaces are one of the most important features introduced in Java 8. They provide a way to define the signature of a single abstract method, which is used to represent a lambda expression. In this blog, we’ll explore what functional interfaces are, why they are useful, and how to use them in Java with code examples.

What are Functional Interfaces?

Functional Interfaces are interfaces that have exactly one abstract method. They are used to represent a single unit of behavior, which can be passed around as an object or used as a lambda expression. Functional Interfaces can also have default and static methods, but these methods do not count towards the one abstract method requirement.

Why are Functional Interfaces useful?

Functional Interfaces are useful because they provide a way to represent behavior in a concise and readable way. They allow us to pass behavior around as objects or lambda expressions, which can be used in many different contexts. They also enable functional programming constructs like higher-order functions and lazy evaluation, which can simplify complex code and improve performance.

How to define and use Functional Interfaces

To define a functional interface in Java, you simply need to create an interface with exactly one abstract method. Here’s an example:

1
2
3
4
5
@FunctionalInterface
interface Calculator {
    int calculate(int x, int y);
}

In this example, we define a functional interface called Calculator with one abstract method calculate. The @FunctionalInterface annotation is optional, but it’s a good practice to include it to make it clear that the interface is intended to be used as a functional interface.

Once we have defined a functional interface, we can use it to create lambda expressions. Here are some examples:

1
2
3
4
5
6
Calculator adder = (x, y) -> x + y;
int sum = adder.calculate(3, 4); // sum is 7

Calculator subtractor = (x, y) -> x - y;
int difference = subtractor.calculate(5, 2); // difference is 3

In these examples, we create lambda expressions that implement the Calculator interface. The lambda expression (x, y) -> x + y adds two integers x and y, and the lambda expression (x, y) -> x - y subtracts y from x. We then use the calculate method to apply the lambda expressions to some arguments and get the result.

Functional Interfaces can also be used as method parameters or return types. Here’s an example:

1
2
3
4
5
6
7
public static int calculate(Calculator calculator, int x, int y) {
    return calculator.calculate(x, y);
}

int sum = calculate((x, y) -> x + y, 3, 4); // sum is 7
int difference = calculate((x, y) -> x - y, 5, 2); // difference is 3

In this example, we define a method called calculate that takes a Calculator as a parameter and applies it to two integers x and y. We can then call this method with different lambda expressions to get different results.

Functional Interfaces can also be combined using default methods to create more complex behavior. Here’s an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@FunctionalInterface
interface UnaryOperation {
    int apply(int x);

    default UnaryOperation compose(UnaryOperation other) {
        return x -> apply(other.apply(x));
    }
}

UnaryOperation square = x -> x * x;
UnaryOperation increment = x -> x + 1;
UnaryOperation squaredIncrement = square.compose(increment);
int result = squaredIncrement.apply(3); // result is 16

Conclusion

Functional Interfaces in Java are a powerful tool for writing concise and readable code. They enable the use of lambda expressions and functional programming constructs, which can simplify complex code and improve performance. By understanding functional interfaces and lambda expressions, you can write code that is more efficient, more maintainable, and easier to read.

This post is licensed under CC BY 4.0 by the author.