Appearance
Java Functional Interfaces ​
Imagine you're baking cookies. You've got your ingredients ready: flour, sugar, eggs, and all that jazz. Now, think of a functional interface as your recipe. It's a blueprint that tells Java how to bake your cookies. But here's the twist: functional interfaces aren't just about baking cookies; they're about getting things done in Java!
In Java, functions are like little blocks of code that do specific tasks. And guess what? Functional interfaces are like the VIP invitations for these functions. They define what these functions should look like. But the coolest part? You can use them to pass around blocks of code just like you'd pass around ingredients for different recipes.
Let's break it down further. Functional interfaces have one and only one abstract method. Wait, don't freak out over the fancy words! Abstract method just means it's a method without any implementation, like a placeholder waiting for you to fill in the code.
java
@FunctionalInterface
interface Calculator {
int operate(int a, int b);
}
public class FunctionalInterfaceExample {
public static void main(String[] args) {
Calculator addition = (a, b) -> a + b;
Calculator subtraction = (a, b) -> a - b;
System.out.println("Addition: " + addition.operate(5, 3)); // Output: Addition: 8
System.out.println("Subtraction: " + subtraction.operate(5, 3)); // Output: Subtraction: 2
}
}
Impressive, isn't it? In our example, we've defined a functional interface called Calculator
with a single abstract method operate()
, which takes two integers and returns an integer result. We then create two instances of Calculator
using lambda expressions—one for addition and one for subtraction.
Now, let's navigate through our code and unravel the magic:
- The
@FunctionalInterface
annotation indicates thatCalculator
is a functional interface, ensuring that it has exactly one abstract method. - Inside our
main()
method, we use lambda expressions to provide implementations for theoperate()
method, making our code concise and expressive. - We then invoke the
operate()
method on ourCalculator
instances, performing addition and subtraction operations with ease.
But wait, there's more to functional interfaces than simple arithmetic! They can be used for a wide range of tasks, from filtering collections to defining custom behaviors for streams and parallel processing.
Another example where we use a functional interface to filter a list of numbers:
java
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class PredicateExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Using a Predicate to filter even numbers
Predicate<Integer> isEven = num -> num % 2 == 0;
List<Integer> evenNumbers = filterList(numbers, isEven);
System.out.println("Even numbers: " + evenNumbers); // Output: Even numbers: [2, 4, 6, 8, 10]
}
static List<Integer> filterList(List<Integer> list, Predicate<Integer> predicate) {
List<Integer> filteredList = new ArrayList<>();
for (Integer num : list) {
if (predicate.test(num)) {
filteredList.add(num);
}
}
return filteredList;
}
}
In this example, we define a Predicate
functional interface to represent a condition for filtering numbers. We then use it to filter even numbers from a list, demonstrating the versatility of functional interfaces in Java.