Scrivio Interview library
ConceptPrincipal7 min read · updated 12 November 2026

Java added lambdas in Java 8 by targeting existing single-method interfaces rather than adding function types to the language. Explain why, what it costs (for example many specialised interfaces in java.util.function), and how that should shape your own API design.

Java 8 made lambdas target existing single-method interfaces so that APIs already built on Runnable, Comparator, Callable and countless library callbacks accepted lambdas immediately without change, and so the language avoided a new structural function type system that would have clashed with erasure-based generics. The cost is that without primitive generics we need many specialised interfaces such as IntFunction and ToLongFunction, and the standard types cannot throw checked exceptions. For our own APIs, we accept standard functional interfaces where they fit, define named ones where domain meaning or checked exceptions matter, and avoid overloading methods on different functional interfaces of the same arity.

Why this question is asked

Many languages have function types written directly in the type system, such as (Int) -> String. Java chose differently: a lambda has no type of its own and always becomes an instance of some interface. That decision shapes daily work, from the 43 interfaces in java.util.function to the occasional ambiguous overload error. Interviewers ask this at principal level because the reasoning behind it is a lesson in evolving a widely used platform, and because it should inform how we design APIs that take behaviour as parameters. In this answer, we will recall how lambdas are typed, explain why the designers chose functional interfaces, count the costs, and turn that into concrete API design guidance.

1A Quick Recap of How Lambdas Are Typed

A lambda expression in Java is a poly expression: its type comes from the context. The context must expect a functional interface, an interface with exactly one abstract method, and the lambda becomes the implementation of that method. The same text can therefore be a Comparator in one place and a BiFunction in another. At run time, invokedynamic creates an object of a class that implements the interface and calls the lambda's body, which javac compiles into a private method.

Java
Runnable r = () -> System.out.println("hi"); // implements run()
Callable<String> c = () -> "hi"; // implements call()
Supplier<String> s = () -> "hi"; // implements get()
System.out.println(c.call() + s.get()); // hihi
// String f = () -> "hi"; // compile error: String is not a functional interface

2Why Functional Interfaces Instead of Function Types

The first reason is the existing ecosystem. By 2014, Java had a huge number of APIs that already took single-method interfaces: Runnable and Callable for concurrency, Comparator for sorting, ActionListener in Swing, FileFilter, and the callback interfaces of frameworks such as Spring's JdbcTemplate. Because lambdas target any interface with one abstract method, every one of those APIs accepted lambdas the day Java 8 shipped, with no recompilation and no new overloads. A new function type would have required adding a second version of every such method, or converting between function types and interfaces everywhere.

The second reason is the type system. Structural function types, where (String) -> int is a type of its own, would need rules for subtyping between function types, variance of parameters and results, and how they appear in generic signatures. Java's generics use erasure: type arguments are removed at compile time, and List<String> and List<Integer> are the same class at run time. A function type like (int) -> String has no natural erased form, and two overloads taking different function types would erase to the same signature. Nominal interfaces fit erasure perfectly, because each interface is just a class name to the JVM.

The design choice and its consequences
AspectFunctional interfaces (Java)Structural function types
Existing APIsAccept lambdas unchangedNeed new overloads or adapters
Fit with erasureInterface name survives erasureNo natural erased form
Names and docsEach type has a name and JavadocShape only
PrimitivesOne interface per primitive shapeWould need primitive generics too
Composing typesPredicate is not a FunctionSame shape, same type

3What the Choice Costs

The biggest cost comes from primitives. Java generics cannot take primitive type arguments, so Function<int, int> is impossible, and Function<Integer, Integer> boxes every value into an object. To avoid boxing in hot code such as IntStream, the JDK added specialised interfaces for common primitive shapes: IntFunction, IntPredicate, ToLongFunction, LongBinaryOperator, ObjIntConsumer and many more. That is why java.util.function contains 43 interfaces, most of them primitive variants, and why the stream API has parallel families like mapToInt, mapToLong and mapToObj.

The second cost is checked exceptions. The standard interfaces declare none, and Java has no way to make an exception type generic across all of them, so lambdas that call throwing methods need wrappers. The third is that shapes do not convert: a Predicate<String> is not a Function<String, Boolean>, even though they mean the same thing, so we sometimes write method references like p::test just to adapt one to the other. Projects such as Valhalla aim to reduce the primitive cost in future Java versions, but for now these costs are part of the language.

Java
IntStream.range(0, 5).map(i -> i * i).sum(); // 30, no boxing: IntUnaryOperator
Stream.of(1, 2, 3).map(i -> i * i).toList(); // [1, 4, 9], boxes each Integer
Predicate<String> empty = String::isEmpty;
Function<String, Boolean> f = empty::test; // adapting one shape to another

4Lesson 1: Accept Standard Types Where They Fit

For our own APIs, the main lesson is that a parameter typed as a standard functional interface is instantly usable with lambdas, method references and any existing function value our callers already have. A generic cache that takes a Function<K, V> loader, or a retry helper that takes a Supplier<T>, should use the standard types, because callers can pass map::get or a lambda with no adaptation.

Where domain meaning, documentation or checked exceptions matter, a named interface is still the better choice, and lambdas work with it equally well. Spring's RowMapper, which declares SQLException, is a good model: the name tells readers what it is for, and the throws clause fits the API.

5Lesson 2: Avoid Overloads on Same-Arity Functional Interfaces

Because a lambda's type comes from its target, overloading a method on two functional interfaces with the same number of parameters can make calls ambiguous, especially with implicitly typed lambdas whose bodies fit both shapes.

Java
void on(Consumer<String> c) { }
void on(Function<String, String> f) { }
on(s -> s.trim()); // compile error: reference to on is ambiguous
// s.trim() can be a statement (Consumer) or a value (Function)
on(s -> { s.trim(); }); // compiles: a block with no return value only fits Consumer
Step throughDesigning a method that takes behaviour
  1. Does a java.util.function type fit the shape and meaning?
    YesUse itNoDefine a named @FunctionalInterface
  2. Is there another overload with the same arity?
    YesRename one of the methodsNoKeep the signature
  3. Will callers need primitives in a hot path?
    YesOffer a primitive-specialised variant with its own nameNoDone

3 steps in order. Play them, or step through them one at a time.

The JDK learned this lesson itself. Comparator.comparing has siblings named comparingInt, comparingLong and comparingDouble instead of overloads, and streams have mapToInt rather than an overloaded map, precisely so lambdas never become ambiguous. ExecutorService.submit, overloaded for Runnable and Callable, is a known source of surprises when a lambda could be either. In our own APIs, the fix is simple: give methods that take different functional shapes different names, such as onEach and transform.

6Follow-up questions

Would Java be better with real function types?

It would remove some costs, such as the primitive variants and adapter method references, but it would have broken the elegant fit with existing APIs and erasure, and it would add a second way to express the same idea. Most practitioners see the functional interface design as the right trade-off for a language with Java's compatibility commitments.

Why are there no TriFunction or QuadFunction types in the JDK?

The JDK stops at two arguments because functions with more parameters usually deserve a name and a domain type, and every extra shape multiplies the primitive variants. If we need three arguments, a named interface or a record parameter is usually clearer.

How do checked exceptions affect API design here?

If the behaviour we accept naturally throws a checked exception, accepting a standard type forces every caller to wrap it. Declaring a named interface with a throws clause, or a generic exception parameter such as interface ThrowingFunction<T, R, E extends Exception>, puts the handling where it belongs, at the caller of our API.

7Conclusion

In this answer, we saw that Java made lambdas target functional interfaces so that existing APIs built on Runnable, Comparator, Callable and library callbacks accepted lambdas without change, and so the language avoided structural function types that fit poorly with erasure. The costs are many primitive-specialised interfaces, no checked exceptions in the standard types and no conversion between equivalent shapes. For our APIs, we accept standard types where they fit, define named interfaces where meaning or checked exceptions matter, and avoid overloads on same-arity functional interfaces.

What to say in the interviewAbout 90 seconds aloud

Open with

Java chose to type lambdas as instances of existing interfaces with one abstract method rather than add function types, and I think the main reason was the ecosystem.

Then, if they want more

There were already thousands of APIs taking Runnable, Callable, Comparator, listeners and framework callbacks, and all of them accepted lambdas the day Java 8 shipped without changes or new overloads.

The second reason is the type system: structural function types need their own subtyping and variance rules, and they don't erase well, while an interface is just a class name to the JVM, which fits erasure-based generics perfectly. The costs are real though.

Generics can't take primitives, so to avoid boxing the JDK needed specialised interfaces, IntFunction, ToLongFunction, IntBinaryOperator and so on; java.util.function has 43 interfaces, and streams have parallel families like mapToInt. The standard types can't throw checked exceptions, and equivalent shapes don't convert, so a Predicate isn't a Function returning Boolean and you adapt with a method reference.

For my own APIs that means: accept standard functional interfaces where the shape fits, because callers can pass lambdas and method references directly. Define named interfaces where domain meaning or checked exceptions matter, like Spring's RowMapper.

And never overload a method on two functional interfaces of the same arity, because an implicitly typed lambda like s -> s.trim() becomes ambiguous; the JDK uses names like comparingInt for exactly that reason.

About this answer

Last updated
12 November 2026
Versions it names
Java 8
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 "Java added lambdas in Java 8 by targeting existing single-method interfaces rather than...": https://scrivio-interview-library.netlify.app/java/interfaces-and-abstract-classes/7762dcec83/