Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
Four ready-made sockets for lambdas: Predicate tests 20 >= 18, Function turns 5 into 15, Consumer prints Hello, Priya, Supplier hands back Monday. Then isPositive.and(isEven), isEven.negate() and multiply.andThen(addFive), which turns 3 and 4 into 17.
What you will learn
- Pick Predicate, Function, Consumer or Supplier by what goes in and what comes out
- Call each one with its method: test, apply, accept or get
- Replace a small custom interface with a built-in shape
- Combine functions with and, negate and andThen, and use BiFunction
Before you start
- Java lambdas and functional interfaces
You can write your own small interfaces for lambdas. But most of them have very common shapes, so Java ships ready-made ones in java.util.function. Think of them as standard power sockets: if your plug matches, it fits.
Four shapes, ready to use
package org.studyeasy.s05l07;
import java.util.function.*; // Predicate, Function, Consumer, Supplier
public class Ex01_FourShapes {
public static void main(String[] args) {
// Predicate: one value in, true or false out
Predicate<Integer> isAdult = age -> age >= 18;
// Function: one value in, a new value out
Function<Integer, Integer> addTen = n -> n + 10;
// Consumer: one value in, nothing back (it just acts)
Consumer<String> greet =
name -> System.out.println("Hello, " + name);
// Supplier: nothing in, a value out
Supplier<String> today = () -> "Monday";
System.out.println("Adult? " + isAdult.test(20));
System.out.println("Plus ten: " + addTen.apply(5));
greet.accept("Priya");
System.out.println("Today: " + today.get());
}
}
Adult? true
Plus ten: 15
Hello, Priya
Today: Monday
Each shape has one method: test, apply, accept or get. A Function has two types: the first is the input, the last the output. A Supplier has empty brackets, because nothing goes in. Predicate<Integer> bad = age -> age + 1; fails, because a Predicate must return a boolean.
The plug that fits the socket
The small interfaces from lambdas and functional interfaces each match a built-in shape:
// Checker, a small interface of our own: an int in, a boolean out
Predicate<Integer> isEven = n -> n % 2 == 0;
// Discount: a double in, a double out
Function<Double, Double> student = total -> total * 0.85;
// a printer: a name in, nothing back
Consumer<String> greet =
name -> System.out.println("Hello, " + name);
// Motto: nothing in, a String out
Supplier<String> motto = () -> "Small step every day";
Is 8 even? true
Student: 1700.0
Hello, Maya
Small step every day
Same results, and not one interface of our own. When the input and output match, a built-in shape fits. A Supplier takes nothing, so Supplier<String> bad = name -> "Hi"; won't compile.
Build a new Predicate from two
Predicate<Integer> isPositive = n -> n > 0;
Predicate<Integer> isEven = n -> n % 2 == 0;
// and: both tests must pass
Predicate<Integer> positiveAndEven = isPositive.and(isEven);
// negate: flips the answer
Predicate<Integer> isOdd = isEven.negate();
System.out.println("4: " + positiveAndEven.test(4));
System.out.println("-4: " + positiveAndEven.test(-4));
System.out.println("7 odd? " + isOdd.test(7));
4: true
-4: false
7 odd? true
and builds a new Predicate where both tests must say true: 4 passes both gates. -4 stops at the first gate, because it isn't positive, and the second test isn't even reached. negate flips one answer: 7 isn't even, so isOdd says true. Both return a new Predicate, not a boolean.
Two values in, then one more step
// BiFunction: two values in, one value out
BiFunction<Integer, Integer, Integer> multiply = (a, b) -> a * b;
Function<Integer, Integer> addFive = n -> n + 5;
// andThen: the result of multiply goes into addFive
BiFunction<Integer, Integer, Integer> multiplyThenAdd =
multiply.andThen(addFive);
System.out.println("3 x 4 = " + multiply.apply(3, 4));
System.out.println("3 x 4 + 5 = " + multiplyThenAdd.apply(3, 4));
3 x 4 = 12
3 x 4 + 5 = 17
A BiFunction is a Function with two inputs: the first two types go in, the last comes out. andThen adds no new logic; it hands multiply's 12 to addFive, which gives 17. multiply.apply(3) fails, because a BiFunction needs two arguments.
Recap
| Shape | In | Out | Method |
|---|---|---|---|
Predicate<T> | one value | boolean | test |
Function<T, R> | one value | a new value | apply |
Consumer<T> | one value | nothing | accept |
Supplier<T> | nothing | a value | get |
BiFunction<T, U, R> | two values | a value | apply |
Using these is optional, but AI tools often write them into code, so it's worth knowing how they read. They're the building blocks of streams.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Java Lambdas and Functional Interfaces
A six-line anonymous Discount becomes total -> total * 0.85. Lambdas with no, one and two parameters show which brackets and braces can go, a Runnable job runs between START and DONE, and two Comparator lambdas sort students by marks, then by name.
Java Optional: Saying Maybe Without null
A phone book returns null for Omar and the next .length() crashes. Optional.ofNullable gives a box instead: Optional[555-1234] or Optional.empty. orElse hands over "no number", ifPresent runs only for Asha, and map adds +1 only when there's a number.
Java Streams I: filter, map and reduce
Picture a conveyor belt: 4, 7, 2, 9, 5 ride through filter(n -> n > 4) and map(n -> n * 10) and come out as [70, 90, 50]. Then sorted and count on exam scores, and reduce folding a cart of 499, 1299, 250 and 799 into a 2847 bill.
Java Streams II: Collectors, groupingBy and flatMap
Six sales feed every example. toList and joining give [Emma, Jake, Sofia] and "North / South / West", groupingBy with counting and summingInt gives a count and a total per region, flatMap pours three orders' items into one list, and mapToInt sums 5400.
