Lambda Built-in Functional Interfaces
Java 8 introduced built-in functional interfaces that are commonly used with lambda expressions. These interfaces provide a concise way to represent common functional patterns. Understanding these interfaces is essential for writing modern, functional-style Java code.
Key Concepts
1. Predicate
The Predicate
interface represents a boolean-valued function of one argument. It is used to test whether a given input satisfies a condition. The Predicate
interface has a single abstract method test(T t)
.
Example
import java.util.function.Predicate; public class PredicateExample { public static void main(String[] args) { Predicate<Integer> isEven = n -> n % 2 == 0; System.out.println(isEven.test(4)); // Output: true System.out.println(isEven.test(7)); // Output: false } }
Analogy
Think of a Predicate
as a gatekeeper who checks if someone is allowed to enter a party. The gatekeeper (predicate) tests each person (input) to see if they meet the criteria (condition) to enter.
2. Consumer
The Consumer
interface represents an operation that accepts a single input argument and returns no result. It is used for performing actions on the input. The Consumer
interface has a single abstract method accept(T t)
.
Example
import java.util.function.Consumer; public class ConsumerExample { public static void main(String[] args) { Consumer<String> printUpperCase = s -> System.out.println(s.toUpperCase()); printUpperCase.accept("hello"); // Output: HELLO } }
Analogy
Consider a Consumer
as a chef who takes ingredients (input) and prepares a dish (performs an action) without returning anything. The chef consumes the ingredients and produces a meal.
3. Function
The Function
interface represents a function that accepts one argument and produces a result. It is used for transforming input into output. The Function
interface has a single abstract method apply(T t)
.
Example
import java.util.function.Function; public class FunctionExample { public static void main(String[] args) { Function<String, Integer> stringLength = s -> s.length(); System.out.println(stringLength.apply("Java")); // Output: 4 } }
Analogy
Think of a Function
as a translator who converts one language (input) into another (output). The translator takes a sentence in one language and produces its translation in another.
4. Supplier
The Supplier
interface represents a supplier of results. It does not take any input but produces a result. The Supplier
interface has a single abstract method get()
.
Example
import java.util.function.Supplier; public class SupplierExample { public static void main(String[] args) { Supplier<Double> randomValue = () -> Math.random(); System.out.println(randomValue.get()); // Output: a random double value } }
Analogy
Consider a Supplier
as a factory that produces goods (result) without needing any raw materials (input). The factory supplies products on demand.
Conclusion
Understanding the built-in functional interfaces Predicate
, Consumer
, Function
, and Supplier
is crucial for leveraging the power of lambda expressions in Java. These interfaces provide a concise way to represent common functional patterns, making your code more readable and maintainable.