Clean • Professional
Java provides a set of ready-made functional interfaces in the package java.util.function. These are called built-in functional interfaces.
They are designed to make lambda expressions and Stream API more powerful, flexible, and reusable in Java programming.
Built-in functional interfaces are predefined interfaces introduced in Java 8 that contain only one abstract method and are mainly used with lambda expressions.
In simple words: Java already provides commonly used functional interfaces so you don’t need to create them again and again.
Java provides 4 main built-in functional interfaces in the java.util.function package that are widely used with Lambda expressions and Stream API.
Used for conditional checks (true/false results).
It is mainly used when you want to test a condition.
Method:
boolean test(T t);
👉 This method returns:
true → if condition is satisfiedfalse → if condition is not satisfiedExample:
import java.util.function.Predicate;
Predicate<Integer> isEven = n -> n % 2 == 0;
System.out.println(isEven.test(10));
Output:
true
Real Use Case:
Function<T, R> is used when you want to take an input and return an output.
It is mainly used for data transformation in Java.
Method
R apply(T t);
👉 This method:
TRExample
import java.util.function.Function;
Function<Integer, Integer> square = x -> x * x;
System.out.println(square.apply(5));
Output
25
Real-World Use Cases
Consumer<T> is used when you want to take input and perform an action, but do not return any result.
It is mainly used for executing operations like printing, logging, etc.
Method
void accept(T t);
👉 This method:
TExample
import java.util.function.Consumer;
Consumer<String> print = name -> System.out.println(name);
print.accept("Java");
Output
Java
Real-World Use Cases
Used to generate or provide a value without taking input.
Method:
T get();
Example:
import java.util.function.Supplier;
Supplier<Double> randomValue = () -> Math.random();
System.out.println(randomValue.get());
Output:
(Random value like 0.4567)
Real-World Use Cases
Java also provides advanced variants:
BiPredicate<Integer, Integer> check = (a, b) -> a > b;
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
BiConsumer<String, Integer> print = (name, age) ->
System.out.println(name + " " + age);
import java.util.function.*;
public class Test {
public static void main(String[] args) {
Predicate<Integer> isEven = n -> n % 2 == 0;
Function<Integer, Integer> square = n -> n * n;
Consumer<Integer> print = n -> System.out.println(n);
Supplier<Integer> supplier = () -> 10;
int value = supplier.get();
if (isEven.test(value)) {
print.accept(square.apply(value));
}
}
}
| Interface | Input | Output | Purpose |
|---|---|---|---|
| Predicate<T> | Yes | Boolean | Condition check |
| Function<T, R> | Yes | Yes | Transformation |
| Consumer<T> | Yes | No | Action |
| Supplier<T> | No | Yes | Value generation |
Built-in functional interfaces are the core building blocks of modern Java functional programming.
They help you write code that is: