Functional InterfacesAdvanced
1Question 1 of 1
javaWhat are Functional Interfaces in Java (Predicate, Function, Consumer, Supplier)?
Explanation & Concept
A Functional Interface has exactly one abstract method and is eligible to be target for lambda expressions. Annotated with `@FunctionalInterface`.
• `Predicate<T>`: `T -> boolean` (filtering) • `Function<T, R>`: `T -> R` (transformation) • `Consumer<T>`: `T -> void` (side-effects) • `Supplier<T>`: `() -> T` (lazy instantiation)
Code Example
java
import java.util.function.*;
public class FunctionalDemo {
public static void main(String[] args) {
Predicate<Integer> isPositive = n -> n > 0;
Function<String, Integer> stringLength = String::length;
Consumer<String> logger = System.out::println;
Supplier<Double> randomGen = Math::random;
if (isPositive.test(10)) {
logger.accept("Length: " + stringLength.apply("Antigravity"));
}
}
}How It Works & Key Takeaways
Functional interfaces form the foundational contracts for the Java Stream API and asynchronous `CompletableFuture` chains.
Mock Test · 1 Questions · Optional
Ready for Optional & Null Safety?
Take a quick test to evaluate yourself, or move straight to the next module.
Advanced1 questions