Stop 09 · Lambdas & StreamsTopics · 2/7

Functional Interfaces

Part of the Java Roadmap

In short

Lambdas need a home, and that home is always a functional interface. Learn the four big ones in java.util.function and you can read almost any stream or Spring code you meet.

A functional interface has exactly one abstract method. java.util.function provides common ones: Predicate (test), Function (apply), Consumer (accept) and Supplier (get).

The jaadu stands at a wall with four differently shaped sockets, happily pushing a matching plug into each one.

Think of it like this

Sockets with one shape

A wall socket accepts any plug with the right shape, whether it is a phone charger, a kettle or a lamp. A functional interface is a socket with exactly one method shape. Any lambda that matches that shape plugs straight in.

Watch it happen

Four interfaces working on one cart

Supplier
Predicate
Function
Consumer

1/4Supplier<Double>.get() takes nothing and hands back a cart total of ₹2,400.

The big ideas

What you need to know

Exactly one abstract method

Default and static methods are allowed, but only one abstract method. That single method is what the lambda implements.

@FunctionalInterface

Optional, but put it on your own interfaces. The compiler then refuses anyone who adds a second abstract method.

The big four

Predicate<T> test, Function<T,R> apply, Consumer<T> accept, Supplier<T> get. Also BiFunction, UnaryOperator and BinaryOperator.

They compose

predicate.and(other), predicate.negate(), function.andThen(next). You can build bigger rules from small ones.

See it in code

Walk through the code

java
1import java.util.function.Consumer;2import java.util.function.Function;3import java.util.function.Predicate;4import java.util.function.Supplier;5 6@FunctionalInterface7interface DiscountRule {8    double apply(double amount);9}10 11public class FunctionalDemo {12    public static void main(String[] args) {13        Supplier<Double> cartTotal = () -> 2400.0;14        Predicate<Double> isBigOrder = amount -> amount >= 2000;15        Function<Double, String> toLabel = amount -> "₹" + amount;16        Consumer<String> printer = text -> System.out.println(text);17        DiscountRule festive = amount -> amount * 0.9;18 19        double total = cartTotal.get();20        if (isBigOrder.test(total)) {21            total = festive.apply(total);22        }23        printer.accept(toLabel.apply(total)); // ₹2160.024    }25}

1/4Your own functional interface. One abstract method, so any (double) -> double lambda fits.

A festive discount built from the four standard interfaces plus one custom functional interface.

Save yourself the pain

Common mistakes

Oops

Calling a Predicate like a method: isBigOrder(total).

Do this instead

Java has no function-call syntax for objects. Call the interface method: isBigOrder.test(total).

Oops

Using Function<Integer, Integer> in a hot loop over millions of numbers.

Do this instead

Use primitive versions like IntUnaryOperator or IntPredicate to avoid boxing every int into an Integer.

Your mission

Build a rules engine

~25 min

Quick check

You need something that takes an Order and returns nothing, just saves it. Which interface fits?

Go deeper

  1. 1Write a method that takes a Predicate<Integer> to filter numbers.
  2. 2Chain Predicates with and(), or() and negate().
  3. 3Create your own interface marked with @FunctionalInterface.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge