Java Functional Interfaces: Predicate, Function, Consumer and Supplier

A Functional Interface (also known as a SAM — Single Abstract Method interface) is an interface that contains strictly one abstract method. Functional interfaces provide the target type definitions for all lambda expressions and method references in Java.

The 4 Built-In Pillars of java.util.function

Interface Method Signature Input → Output Primary Real-World Use Case
Predicate<T> boolean test(T t) One Object → boolean Filtering data and evaluating conditional rules
Function<T, R> R apply(T t) One Object (T) → One Object (R) Transforming or mapping data types
Consumer<T> void accept(T t) One Object (T) → void Consuming data (printing, logging, saving)
Supplier<T> T get() No Arguments → One Object (T) Factory methods, generating timestamps/IDs

Comprehensive Code Demonstration: All 4 In Action

import java.util.function.Predicate;
import java.util.function.Function;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.time.LocalDateTime;

public class FunctionalInterfacesDemo {
    public static void main(String[] args) {
        // 1. Predicate: Check if number is positive
        Predicate<Integer> isPositive = n -> n > 0;
        System.out.println("Is 15 positive? " + isPositive.test(15)); // true

        // 2. Function: Convert String to its Character Length
        Function<String, Integer> lengthMapper = s -> s.length();
        System.out.println("Length of 'Veewom': " + lengthMapper.apply("Veewom")); // 6

        // 3. Consumer: Print greeting to console
        Consumer<String> greeter = name -> System.out.println("Hello, " + name + "!");
        greeter.accept("Aarav"); // Hello, Aarav!

        // 4. Supplier: Produce current timestamp
        Supplier<LocalDateTime> clock = () -> LocalDateTime.now();
        System.out.println("Current Time: " + clock.get());
    }
}

Next Step

Now let us explore the revolutionary Java Stream API: Declarative, pipeline-based data processing. Proceed to: Java Stream API & Pipelines →

Frequently Asked Questions (FAQ)

Q1. What defines a Functional Interface in Java?
A Functional Interface is an interface that contains EXACTLY ONE abstract method (Single Abstract Method / SAM). It can have any number of default or static methods.
Q2. What is the role of the @FunctionalInterface annotation?
The @FunctionalInterface annotation is an informative compiler check that ensures the interface possesses exactly one abstract method. If a second abstract method is added, the compiler throws an error.
Q3. What are the 4 core built-in functional interfaces in java.util.function?
The 4 core interfaces are: 1. Predicate (takes T, returns boolean), 2. Function (takes T, returns R), 3. Consumer (takes T, returns void), and 4. Supplier (takes nothing, returns T).

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Lambda Expressions →

Implementing functional interfaces.

Java Stream API →

Passing predicates and functions to streams.

Java Optional Class →

Using Supplier and Consumer.

Java Interfaces →

Default and static methods.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!