Sub topic 15
When Lambdas Make Overloads Ambiguous
You already know that a lambda has no type of its own. The compiler gives it a type from the place where you use it. That place is called the target type. Usually this works without any help from you. This chapter covers the case where it doesn't: a method that is overloaded with more than one functional interface.
What Is an Overload?
An overload is a second method with the same name but different parameter types. Java picks the right version by looking at the arguments you pass. With a plain value like 5 or "hi", this choice is easy, because the value already has a type.
A lambda is harder. The same lambda can fit many functional interfaces. If two overloads both accept it, the compiler may not be able to choose.
A Call the Compiler Rejects
Here are two process methods. One takes a Function<String, Integer>. The other takes a ToIntFunction<String>.
import java.util.function.Function;
import java.util.function.ToIntFunction;
public class Overloads {
static void process(Function<String, Integer> f) {
System.out.println("Function: " + f.apply("hello"));
}
static void process(ToIntFunction<String> f) {
System.out.println("ToIntFunction: " + f.applyAsInt("hello"));
}
public static void main(String[] args) {
// process(s -> s.length());
// error: reference to process is ambiguous
process((Function<String, Integer>) s -> s.length()); // Function: 5
process((ToIntFunction<String>) s -> s.length()); // ToIntFunction: 5
ToIntFunction<String> len = s -> s.length();
process(len); // ToIntFunction: 5
}
}The lambda s -> s.length() takes one argument and returns a value. Both interfaces match that shape, and the compiler cannot rank one above the other. It stops with an "ambiguous" error and does not guess.
Ways to Fix It
You fix the error by giving the compiler a clear target type.
| Fix | Example | When to use it |
|---|---|---|
| Cast the lambda | `process((ToIntFunction<String>) s -> s.length())` | A quick fix at one call site |
| Store it in a typed variable | `ToIntFunction<String> len = ...; process(len);` | You want readable code or will reuse the lambda |
| Rename the methods | `processToInt(...)` and `processToObject(...)` | You own the API |
Renaming is the best fix when you write the methods yourself. The JDK does this too. Comparator has comparingInt, comparingLong, and comparingDouble instead of more overloads of comparing, so lambdas passed to them are never ambiguous.
Exercises
Exercise
A class has two methods, run(Supplier<String> s) and run(Callable<String> c). Why does run(() -> "done") fail to compile, and what is one way to fix it?
Show answer
Both Supplier<String> and Callable<String> take no arguments and return a String, so the lambda fits both overloads and neither is more specific. The compiler reports an ambiguous call. You can fix it with a cast, such as run((Supplier<String>) () -> "done"), or by assigning the lambda to a Supplier<String> variable first and passing that variable.
Exercise
You are designing a library and want one method that accepts a Function<T, Integer> and another that accepts a ToIntFunction<T>. What naming choice avoids ambiguity for your users?
Show answer
Give the methods different names, for example map and mapToInt, the way Comparator uses comparing and comparingInt. Each lambda then has only one possible target type.