Course Content
Stream API
Stream API
What Is a Functional Interface?
It's time to take a deep dive into how lambdas are created in Java and what functional interfaces have to do with them.
A functional interface can be used to represent an action or operation that can be executed. Instead of creating an entire class for a simple task, you can define an interface with a single method that describes the action.
Example of a functional interface:
The @FunctionalInterface
annotation is optional, but it helps the compiler and developers by enforcing that the interface remains functional and preventing the addition of multiple abstract methods.
Why Do We Need Them?
In Stream API, functional interfaces are used to pass different behaviors into methods that work with collections of data.
For example, you can pass a filtering condition to a method that selects specific elements from a list or provide an operation to transform each element, as seen in methods like filter()
and map()
.
Main
package com.example; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); // Using `filter()` to extract even numbers List<Integer> evenNumbers = numbers.stream() .filter(n -> n % 2 == 0) // Predicate: keeps only even numbers .collect(Collectors.toList()); System.out.println(evenNumbers); } }
The filter()
method takes a special functional interface called Predicate<T>
(which you'll cover later), which checks if an element meets a certain condition. In this case, the lambda expression n -> n % 2 == 0
determines whether a number is even. If the number is divisible by 2 without a remainder, it stays in the stream.
Creating a Custom Functional Interface
Let's say you need an interface to perform arithmetic operations like addition or subtraction.
You can create a functional interface called ArithmeticOperation
with a method apply
that performs an operation on two numbers, a
and b
.
Now that you have the interface, you can use lambda expressions to define different operations like addition and subtraction:
Main
public class Main { public static void main(String[] args) { // Addition ArithmeticOperation add = (a, b) -> a + b; System.out.println("Sum: " + add.apply(5, 3)); // 8 // Subtraction ArithmeticOperation subtract = (a, b) -> a - b; System.out.println("Difference: " + subtract.apply(5, 3)); // 2 } }
The lambda expressions (a, b) -> a + b
and (a, b) -> a - b
implement the apply
method of the interface, performing addition and subtraction respectively. The first lambda expression returns the sum of two numbers, while the second returns their difference.
This approach provides a flexible way to use different arithmetic operations without the need to create separate classes for each one.
1. What is a functional interface in Java?
2. Which lambda expression correctly implements the following functional interface?
Thanks for your feedback!