Course contentsShow
← Java
Lesson 1741 of 2,87036. Lambda Expressions and Method ReferencesFree lesson

Method References with Multiple Parameters

See how method references work with methods accepting multiple arguments matching the functional interface signature.

Method References with Multiple Parameters

You'll learn how method references cleanly handle methods that accept multiple arguments, as long as they match the functional interface signature.

The Core Idea

Method references aren't limited to single-parameter methods. When a functional interface expects multiple parameters, you can use a method reference to any method whose parameter list matches exactly—same number of arguments, compatible types, and correct order.

Think of it like plugging a cable into a socket: the shape (parameter signature) must match perfectly. If your functional interface needs (String, Integer), your referenced method must accept exactly those types in that order.

How It Works

The functional interface defines the expected signature. Common multi-parameter interfaces include:

  • BiFunction<T, U, R> – two inputs, one output
  • BiConsumer<T, U> – two inputs, no return
  • BiPredicate<T, U> – two inputs, returns boolean

When you write a method reference, Java automatically maps the interface's parameters to the method's parameters positionally.

Example:

BiFunction<String, String, Integer> indexOf = String::indexOf;
// Equivalent to: (str, substring) -> str.indexOf(substring)

int position = indexOf.apply("Hello World", "World"); // Returns 6

Here, indexOf expects two arguments: the first becomes the object (str), the second becomes the method argument (substring).

With BiConsumer:

BiConsumer<StringBuilder, String> appender = StringBuilder::append;
// Equivalent to: (sb, text) -> sb.append(text)

StringBuilder sb = new StringBuilder("Hello");
appender.accept(sb, " Java"); // sb now contains "Hello Java"

When to Use

Choose method references with multiple parameters when the existing method signature perfectly matches your functional interface—it eliminates lambda boilerplate and improves readability.

Key Takeaway: Method references work seamlessly with multi-parameter functional interfaces when the method's signature (parameter types and order) exactly matches the interface's abstract method.