Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

Name the core functional interfaces in java.util.function and their method signatures. Why do primitive specialisations such as IntPredicate exist?

The four core interfaces are Function<T,R> with R apply(T), Consumer<T> with void accept(T), Supplier<T> with T get(), and Predicate<T> with boolean test(T). UnaryOperator<T> and BinaryOperator<T> are specialisations of Function and BiFunction where all types are the same, and the Bi variants, BiFunction, BiConsumer and BiPredicate, take two arguments. Primitive specialisations such as IntPredicate, ToLongFunction and IntBinaryOperator exist because generics cannot use primitive types, so they avoid boxing, and composition methods such as andThen, compose, and, or and negate build bigger functions from small ones.

Why this question is asked

Every stream pipeline, every Optional chain and many Spring APIs are built from the interfaces in java.util.function. We use them constantly, often without noticing which one we are using, until we need to write a method that accepts one, or a compiler error mentions ToIntFunction. Interviewers ask this question to make sure we know the core shapes by heart, understand why there are 43 interfaces instead of four, and can combine them. In this answer, we will go through the four core interfaces, the operator and two-argument variants, the primitive specialisations and the boxing they avoid, and the composition methods, with code for each.

1The Four Core Shapes

Each core interface describes one shape of function, and the names describe the role. A Function takes something and returns something. A Consumer takes something and returns nothing, so it exists for its side effect. A Supplier takes nothing and returns something, which makes it useful for lazy or repeated creation. A Predicate takes something and returns a boolean, which is what filters need.

Java
Function<String, Integer> length = s -> s.length();
System.out.println(length.apply("lambda")); // 6
Consumer<String> log = s -> System.out.println("log: " + s);
log.accept("started"); // log: started
Supplier<List<String>> fresh = ArrayList::new;
System.out.println(fresh.get().size()); // 0, a new list on each call
Predicate<String> blank = String::isBlank;
System.out.println(blank.test(" ")); // true
The core interfaces
InterfaceMethodTypical use
Function<T,R>R apply(T t)map, transform a value
Consumer<T>void accept(T t)forEach, side effects
Supplier<T>T get()lazy values, factories, orElseGet
Predicate<T>boolean test(T t)filter, removeIf, anyMatch
UnaryOperator<T>T apply(T t)replaceAll, iterate
BinaryOperator<T>T apply(T a, T b)reduce, merge functions
BiFunction<T,U,R>R apply(T t, U u)Map.compute, combining two inputs
BiConsumer<T,U>void accept(T t, U u)Map.forEach
BiPredicate<T,U>boolean test(T t, U u)two-argument checks

Notice how each one matches a stream operation: map takes a Function, forEach takes a Consumer, filter takes a Predicate, and Stream.generate or Collectors.toCollection take a Supplier. Knowing the shape tells us which operation accepts it.

2Operators and Two-Argument Variants

UnaryOperator<T> is a Function<T, T>: input and output have the same type. It extends Function, so it can be used anywhere a Function<T, T> is expected, and the name tells the reader that the type does not change. List.replaceAll takes one. BinaryOperator<T> is likewise a BiFunction<T, T, T>, and it is the type that reduce and Map.merge expect.

The Bi variants simply take two arguments. BiFunction<T, U, R> takes a T and a U and returns an R, BiConsumer takes two and returns nothing, which is why Map.forEach uses it for key and value, and BiPredicate tests two values. There are no Tri variants in the JDK; for three arguments we define our own interface.

Java
UnaryOperator<String> upper = String::toUpperCase;
List<String> names = new ArrayList<>(List.of("ana", "bo"));
names.replaceAll(upper); // [ANA, BO]
BinaryOperator<Integer> add = Integer::sum;
System.out.println(Stream.of(1, 2, 3).reduce(0, add)); // 6
Map<String, Integer> stock = new HashMap<>(Map.of("apple", 3));
stock.merge("apple", 2, Integer::sum); // {apple=5}
stock.forEach((k, v) -> System.out.println(k + "=" + v)); // BiConsumer: apple=5
BiFunction<String, Integer, String> repeat = String::repeat;
System.out.println(repeat.apply("ab", 3)); // ababab

3Why Primitive Specialisations Exist

Java generics only work with reference types, so a Function<Integer, Integer> must take and return Integer objects, not ints. Every call boxes the int into an Integer and unboxes it again. Integer.valueOf caches the values from -128 to 127, but outside that range each boxing creates a new object, and in a hot loop over millions of values that means millions of short-lived allocations and extra memory reads.

Java
Function<Integer, Integer> boxed = x -> x * 2; // boxes the argument and the result
IntUnaryOperator prim = x -> x * 2; // int in, int out, no objects
long sum1 = 0;
for (int i = 0; i < 10_000_000; i++) sum1 += boxed.apply(i); // up to 2 allocations per call
long sum2 = 0;
for (int i = 0; i < 10_000_000; i++) sum2 += prim.applyAsInt(i); // none
System.out.println(sum1 == sum2); // true; the second loop is usually much faster
IntPredicate even = n -> n % 2 == 0; // test(int)
ToLongFunction<Order> cents = Order::amountInCents; // applyAsLong(Order) returns long
IntBinaryOperator max = Math::max; // applyAsInt(int, int)

Notice the method names change too: applyAsInt, applyAsLong, test(int). That is how the interfaces avoid boxing at both ends. The family covers int, long and double only, in a few patterns: IntFunction<R> takes an int and returns an object, ToIntFunction<T> takes an object and returns an int, IntToLongFunction converts between primitives, and IntUnaryOperator and IntBinaryOperator keep the type. There are no specialisations for byte, short, char or float, and for boolean only BooleanSupplier, which is part of why java.util.function has 43 interfaces in total.

In practice, the JIT can sometimes remove boxing through escape analysis, but we cannot rely on it. IntStream, LongStream and DoubleStream exist for the same reason, and mapToInt is the bridge from a Stream<T> to them.

4Building Functions With Composition Methods

The interfaces also have default and static methods that combine small functions into bigger ones, so we do not have to write a new lambda for every combination.

Java
Function<String, String> trim = String::trim;
Function<String, Integer> len = String::length;
Function<String, Integer> trimmedLength = trim.andThen(len); // first trim, then length
System.out.println(trimmedLength.apply(" hi ")); // 2
Function<String, Integer> same = len.compose(trim); // compose runs trim first too
System.out.println(same.apply(" hi ")); // 2
Predicate<String> notBlank = Predicate.not(String::isBlank); // Java 11
Predicate<String> shortWord = s -> s.length() < 5;
Predicate<String> both = notBlank.and(shortWord);
System.out.println(both.test("cat")); // true
System.out.println(both.negate().test("cat")); // false
System.out.println(shortWord.or(String::isEmpty).test("")); // true
Consumer<String> audit = s -> System.out.println("audit " + s);
Consumer<String> save = s -> System.out.println("save " + s);
save.andThen(audit).accept("x"); // save x, then audit x
BinaryOperator<String> shorter = BinaryOperator.minBy(Comparator.comparingInt(String::length));
System.out.println(shorter.apply("pear", "fig")); // fig

andThen and compose differ only in order: f.andThen(g) applies f first, while f.compose(g) applies g first. Predicates combine with and, or and negate, which short-circuit like && and ||. Function.identity() returns a function that gives back its input, handy for toMap(Function.identity(), ...).

5Follow-up questions

Why are there no primitive versions for every combination?

The number of interfaces would explode. The designers covered int, long and double, which cover nearly all numeric work, since smaller types widen to int and float to double. The remaining gaps are filled by our own small interfaces when needed, and Project Valhalla aims to make many of these specialisations unnecessary in the future.

Can I use Callable or Runnable instead of Supplier or a Consumer?

They are functional interfaces too, and a lambda can target them. Callable is like a Supplier that may throw a checked exception, and Runnable takes and returns nothing. Choose the one whose meaning fits: Runnable and Callable for tasks, java.util.function types for data transformations.

What is the difference between Function<T,T> and UnaryOperator<T>?

Behaviour is identical, since UnaryOperator extends Function. UnaryOperator documents that the type is unchanged and is what APIs such as List.replaceAll and Stream.iterate require, while a Function<T,T> would not be accepted by replaceAll.

6Conclusion

In this answer, we saw the four core shapes in java.util.function: Function<T,R> with apply, Consumer<T> with accept, Supplier<T> with get and Predicate<T> with test, plus UnaryOperator and BinaryOperator as same-type specialisations of Function and BiFunction, and the two-argument Bi variants. Primitive specialisations such as IntPredicate, ToLongFunction and IntBinaryOperator exist because generics cannot take primitives, so they avoid boxing on every call. Finally, andThen, compose, and, or, negate and helpers such as Predicate.not let us build larger functions from small ones.

What to say in the interviewAbout 80 seconds aloud

Open with

The four core interfaces are Function<T,R>, whose method is R apply(T), Consumer<T> with void accept(T), Supplier<T> with T get(), and Predicate<T> with boolean test(T). They match stream operations directly: map takes a Function, forEach a Consumer, filter a Predicate, and things like orElseGet or toCollection take a Supplier.

Then, if they want more

UnaryOperator<T> is a Function from T to T and BinaryOperator<T> is a BiFunction of two Ts returning T; they are what replaceAll, reduce and Map.merge expect. The Bi variants, BiFunction, BiConsumer and BiPredicate, take two arguments, which is why Map.forEach takes a BiConsumer. There is nothing for three arguments in the JDK.

The primitive specialisations exist because generics only work with reference types. A Function<Integer, Integer> boxes the input and the output on every call, and outside the small Integer cache that means allocations in hot loops.

So there are versions like IntPredicate with test(int), ToLongFunction with applyAsLong, IntUnaryOperator and IntBinaryOperator, covering int, long and double, which together with the rest makes 43 interfaces.

Finally, they have composition methods: andThen and compose on Function, andThen on Consumer, and, or and negate on Predicate, plus Predicate.not and Function.identity, so I can build a filter like notBlank.and(shortWord) without writing a new lambda.

About this answer

Last updated
12 November 2026
Versions it names
Java 11
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Name the core functional interfaces in java.util.function and their method signatures....": https://scrivio-interview-library.netlify.app/java/lambdas/a0f32003ec/