Sub topic 5
The Core Toolkit: Function, Predicate, Consumer, and Supplier
In the last chapters you wrote your own functional interfaces and let the compiler work out each lambda's type. You will rarely need your own interface for common jobs. Java already provides a set of ready-made ones in the package java.util.function. A package is a named group of related classes and interfaces.
This chapter covers the six interfaces you will use most often.
The Big Four and Two Helpers
Each interface has exactly one abstract method, so a lambda can stand in for it. What separates them is what goes in and what comes out.
| Interface | Takes | Returns | Method name | Typical use |
|---|---|---|---|---|
| `Function<T, R>` | one `T` | an `R` | `apply` | Turn one value into another |
| `Predicate<T>` | one `T` | `boolean` | `test` | Ask a yes/no question |
| `Consumer<T>` | one `T` | nothing | `accept` | Do something with a value |
| `Supplier<T>` | nothing | a `T` | `get` | Produce a value on demand |
| `UnaryOperator<T>` | one `T` | a `T` | `apply` | Change a value, same type |
| `BinaryOperator<T>` | two `T`s | a `T` | `apply` | Combine two values |
The letters T and R are type parameters. A type parameter is a placeholder you fill in with a real type, the same way List<String> fills in String. T usually means the input type and R the result type.
A Worked Example
The program below creates one lambda for each interface and then calls it.
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
public class BuiltInInterfaces {
public static void main(String[] args) {
Function<String, Integer> length = s -> s.length();
Predicate<Integer> isEven = n -> n % 2 == 0;
Consumer<String> printer = s -> System.out.println(s);
Supplier<String> greeting = () -> "Hello";
UnaryOperator<String> shout = s -> s.toUpperCase();
BinaryOperator<Integer> max = (a, b) -> Math.max(a, b);
System.out.println(length.apply("lambda")); // 6
System.out.println(isEven.test(4)); // true
printer.accept(greeting.get()); // Hello
System.out.println(shout.apply("hi")); // HI
System.out.println(max.apply(3, 7)); // 7
}
}Here is what each line does:
lengthtakes aStringand gives back its length as anInteger.isEvenanswers "is this number even?" withtrueorfalse.printerprints its input and returns nothing.greetingtakes no input. Its()is an empty parameter list.shoutis aFunctionwhose input and output are bothString.UnaryOperator<String>is a shorter way to write that.maxcombines two numbers into one.
Why Use the Standard Ones?
Other Java code expects these interfaces. Many library methods take a Predicate or a Consumer as a parameter, and you will see that in the chapter on collections. If your lambdas already use these standard types, they plug straight into those methods. Other programmers also recognize the names at a glance, which makes your code easier to read.
Exercise
You need a lambda that checks whether a String is longer than 10 characters. Which interface should you use, and how would you write it?
Show answer
Use Predicate<String>, because it takes one value and returns a boolean: Predicate<String> isLong = s -> s.length() > 10; and call it with isLong.test("some text").
Exercise
You need a lambda that creates a new random number each time you ask for one, with no input. Which interface fits, and what method do you call?
Show answer
Supplier<Double>, for example Supplier<Double> random = () -> Math.random(); and you call random.get().