Skip to main content
PreparationZone Logo
PrepZone

Built-in Functional Interfaces

Function, Predicate, Consumer, Supplier — compose them without reinventing SAM types.

Pass checkpoint below

The shapes interviewers name

AspectInterfaceShape and typical use
Function<T,R>T in, R outmap: transform
Predicate<T>T in, boolean outfilter
Consumer<T>T in, voidforEach side effects
Supplier<T>nothing in, T outlazy creation
UnaryOperator<T>T in, T outreplaceAll
BinaryOperator<T>two T in, T outreduce
BiFunction<T,U,R>two inputs, one outputmerge / compute
  • Function<T,R>

    InterfaceT in, R out
    Shape and typical usemap: transform
  • Predicate<T>

    InterfaceT in, boolean out
    Shape and typical usefilter
  • Consumer<T>

    InterfaceT in, void
    Shape and typical useforEach side effects
  • Supplier<T>

    Interfacenothing in, T out
    Shape and typical uselazy creation
  • UnaryOperator<T>

    InterfaceT in, T out
    Shape and typical usereplaceAll
  • BinaryOperator<T>

    Interfacetwo T in, T out
    Shape and typical usereduce
  • BiFunction<T,U,R>

    Interfacetwo inputs, one output
    Shape and typical usemerge / compute

Prefer IntPredicate, ToIntFunction, etc. on hot paths to avoid boxing.

Java
public class Example {
    public static void main(String[] args) {
        Predicate<String> notBlank = text -> !text.isBlank();
            Function<String, Integer> length = String::length;
            Supplier<List<String>> newList = ArrayList::new;

            Predicate<String> valid = notBlank.and(text -> text.length() <= 20);
            Function<String, Integer> stripThenLength = ((Function<String, String>) String::strip).andThen(String::length);
    }
}

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 2 sections

  • Your own functional interface
  • Predicate helpers (Java 11+)

Test yourself

Answer these before moving on — recall is what makes it stick.