Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Java has no function types and instead uses many functional interfaces, including primitive specialisations. How does this affect library design and interoperability over years, and what would you standardise in your libraries?

Because Java has no function types, every API must choose a named interface for each function it accepts, and different libraries choose different ones for the same shape, so callers keep writing adapters, usually method references such as f::apply. The primitive specialisations exist to avoid boxing until value types arrive, but they multiply the number of types. In our libraries I would standardise on java.util.function for generic hooks plus a small set of named domain interfaces, and forbid parallel sets of generic interfaces.

Why this question is asked

Most of the time we do not notice that Java lacks function types, because a lambda fits any functional interface with the right shape. The cost shows up over years, at the seams between libraries: a Guava function here, a Spring Converter there, an internal Func1 from an old framework, and dozens of small adapters that exist only to turn one into another. Interviewers ask this of very senior engineers because the decision about which interfaces a company's libraries expose lasts a decade and is expensive to undo. In this answer, we will recap what target typing means, see how the same shape ends up in many types, look at why the primitive specialisations exist, and then decide what we would standardise and how.

1A Quick Recap: Lambdas Have No Type of Their Own

In languages with function types, such as Kotlin or Scala, (String) -> Int is a type in its own right. Any function from String to Int has that type, and any API that accepts one accepts all of them.

Java took a different route in Java 8. A lambda has no type until the context gives it one. This is called target typing: the compiler looks at the type expected where the lambda appears, which must be a functional interface (an interface with one abstract method), and turns the lambda into an instance of that interface. The same text s -> s.length() can become a Function<String, Integer>, a ToIntFunction<String> or our own Measure interface, depending only on where we write it.

The design reused the huge number of existing single-method interfaces, such as Runnable, Comparator and Callable, so old APIs became lambda-friendly overnight. That was a good trade in 2014. The consequence is that the shape of a function and its type are two separate things.

2Same Shape, Different Types

Once a lambda has been assigned to a variable, it has a fixed type, and a different interface with the same shape is not compatible with it.

Java
Function<String, Integer> parse = Integer::parseInt;
ToIntFunction<String> parseInt = parse; // compile error: incompatible types
ToIntFunction<String> adapted = parse::apply; // compiles: a new object that calls parse
Converter<String, Integer> spring = parse::apply; // Spring's Converter, same shape again
int n = adapted.applyAsInt("42"); // 42
One shape, many interfaces
InterfaceComes fromNotes
java.util.function.Function<T, R>JDK 8the common default
com.google.common.base.FunctionGuavaextends the JDK Function since Guava 21
Converter<S, T>Spring Frameworktype conversion, may return null
io.reactivex.functions.FunctionRxJava 2 and 3apply declares throws Exception
Func1, Mapper and similarolder internal librariesa parallel set that needs adapters everywhere
Each library picked its own type for a function from T to R.

Notice that the fix is always the same small move: a method reference to the single method of the existing object, which the compiler wraps in a new instance of the target interface. It is cheap to write, but every adapter is an extra object, an extra frame in stack traces and an extra place where a reader must stop and think. In a large codebase they pile up at every boundary between libraries.

3Why There Are So Many Primitive Specialisations

Java generics work only with reference types, so Function<Integer, Integer> must box every int into an Integer object and unbox it again. In a tight loop over millions of values that means millions of short-lived objects and extra garbage collection. To avoid this, java.util.function contains 43 interfaces, most of them primitive variants such as IntUnaryOperator, ToLongFunction<T> and IntPredicate.

Java
Function<Integer, Integer> boxed = x -> x * 2; // boxes the argument and the result
IntUnaryOperator primitive = x -> x * 2; // works on int, no objects created
int sum = IntStream.range(0, 1_000).map(primitive).sum(); // 999000, with no Integer objects created

But there is a catch: the variants only cover int, long and double, and only some combinations. There is no CharPredicate and no IntToIntBiFunction, so library authors either box anyway or invent their own interfaces, which brings back the many-types problem. The real fix being worked on is Project Valhalla, which aims to bring value types and generics over primitives so a single Function could work without boxing. Until that ships, check the status for your Java version, the specialisations are a performance tool to use only where profiling shows boxing matters.

4What This Means for Our Libraries Over the Years

Every public signature in a shared library chooses an interface, and that choice spreads. If our core library takes Func1, every team writes Func1 lambdas, every integration with Spring or the JDK needs an adapter, and removing Func1 later means a breaking change in hundreds of places. If it takes java.util.function.Function, it works directly with streams, Optional, CompletableFuture and almost every modern library.

So the long-term cost is not the adapter itself; it is the number of seams. The fewer distinct interfaces our libraries expose for the same idea, the fewer seams there are.

5What I Would Standardise

I would write down three rules. First, generic hooks use java.util.function types, including the primitive variants only where boxing is a measured cost. Second, domain concepts get a small number of named interfaces, such as PricingRule or RetryPolicy, because there the name carries meaning that the generic type cannot. Third, nobody introduces a parallel set of generic interfaces such as Func1, Action2 or a homemade Supplier. If a team needs a throwing variant, there is exactly one shared module that defines it, so we do not end up with five ThrowingFunction types.

Step throughChoosing the type for a new functional parameter
  1. Is it a domain concept?
    YesNamed @FunctionalInterfaceNoUse java.util.function
  2. Is boxing a measured cost?
    YesUse the primitive variantNoUse the generic type
  3. Must it throw checked exceptions?
    YesUse the one shared throwing typeNoDone
  4. Never add a new generic familyNo Func1, Action2 or local Supplier

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

The aim is fewer distinct types for the same idea across all our libraries.

For existing libraries that already expose a parallel set, we do not rewrite everything at once. We add overloads or new methods that take the standard types, mark the old ones as deprecated, provide a single adapter class during the transition, and remove the old interfaces in a major version once usage has dropped. An architecture test, for example an ArchUnit rule, can stop new public methods from taking the deprecated types.

6Follow-up questions

Why did Java not just add function types in Java 8?

Function types would have needed a new kind of type in the type system, new rules for generics and erasure, and a way to connect them to the thousands of existing single-method interfaces. Reusing functional interfaces gave lambdas to every old API immediately, with no changes to the JVM type system, and the designers judged that worth the cost of many named types.

Is an adapter like parse::apply expensive at runtime?

It costs one small object and an extra call, which the JIT compiler usually inlines. The real cost is in readability and in stack traces, not CPU. It only matters for performance in very hot loops, where it is usually combined with boxing that is the bigger cost.

When is a custom generic interface justified?

Almost only for checked exceptions, because the standard interfaces cannot throw them. Even then, the company should have exactly one such type per shape in one shared module, documented and reused, instead of every team writing its own.

7Conclusion

In this answer, we saw that Java lambdas take their type from the context, so the same function shape exists as many unrelated interfaces across the JDK, Guava, Spring, RxJava and internal libraries. Crossing between them needs adapters, usually method references, and the primitive specialisations add more types to avoid boxing until Valhalla delivers value types. For our own libraries, the answer is to standardise on java.util.function for generic hooks, keep a few named domain interfaces, allow one shared throwing variant, and migrate away from any parallel generic families with deprecation and architecture tests.

What to say in the interviewAbout 90 seconds aloud

Open with

Java chose target typing instead of function types: a lambda gets its type from the functional interface expected where it appears. That let all the existing single-method interfaces work with lambdas immediately, but it means the same shape, say a function from String to Integer, exists as many incompatible types: java.util.function Function, Guava's Function, Spring's Converter, RxJava's Function, and often an internal Func1.

Then, if they want more

Once a lambda is stored in one type, passing it to another library needs an adapter, usually a method reference like f colon colon apply. Each adapter is cheap, but over years the number of seams between libraries becomes the real cost, in readability, stack traces and migrations.

The primitive specialisations, like IntUnaryOperator and ToLongFunction, exist to avoid boxing because generics only work with objects. They only cover int, long and double and only some combinations, so they multiply types, and they should be used where profiling shows boxing matters, until Valhalla gives us value types.

In our libraries I would standardise on java.util.function for generic hooks, a small set of named interfaces for domain concepts where the name adds meaning, and exactly one shared module for throwing variants.

I would forbid new parallel families of generic interfaces, migrate old ones by adding methods with standard types, deprecating the old ones, and enforcing it with an architecture test.

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 has no function types and instead uses many functional interfaces, including...": https://scrivio-interview-library.netlify.app/java/lambdas/867e132437/