Lambdas are purely a call-site construct: the recipient of the lambda does not need to know that a Lambda is involved, instead it accepts an Interface with the appropriate method.

In other words, you define or use a functional interface (i.e. an interface with a single method) that accepts and returns exactly what you want.

Since Java 8 there is a set of commonly-used interface types in java.util.function.

For this specific use case there's java.util.function.IntBinaryOperator with a single int applyAsInt(int left, int right) method, so you could write your method like this:

static int method(IntBinaryOperator op){
    return op.applyAsInt(5, 10);
}

But you can just as well define your own interface and use it like this:

public interface TwoArgIntOperator {
    public int op(int a, int b);
}

//elsewhere:
static int method(TwoArgIntOperator operator) {
    return operator.op(5, 10);
}

Then call the method with a lambda as parameter:

public static void main(String[] args) {
    TwoArgIntOperator addTwoInts = (a, b) -> a + b;
    int result = method(addTwoInts);
    System.out.println("Result: " + result);
}

Using your own interface has the advantage that you can have names that more clearly indicate the intent.

Answer from Joachim Sauer on Stack Overflow
Top answer
1 of 16
337

Lambdas are purely a call-site construct: the recipient of the lambda does not need to know that a Lambda is involved, instead it accepts an Interface with the appropriate method.

In other words, you define or use a functional interface (i.e. an interface with a single method) that accepts and returns exactly what you want.

Since Java 8 there is a set of commonly-used interface types in java.util.function.

For this specific use case there's java.util.function.IntBinaryOperator with a single int applyAsInt(int left, int right) method, so you could write your method like this:

static int method(IntBinaryOperator op){
    return op.applyAsInt(5, 10);
}

But you can just as well define your own interface and use it like this:

public interface TwoArgIntOperator {
    public int op(int a, int b);
}

//elsewhere:
static int method(TwoArgIntOperator operator) {
    return operator.op(5, 10);
}

Then call the method with a lambda as parameter:

public static void main(String[] args) {
    TwoArgIntOperator addTwoInts = (a, b) -> a + b;
    int result = method(addTwoInts);
    System.out.println("Result: " + result);
}

Using your own interface has the advantage that you can have names that more clearly indicate the intent.

2 of 16
78

To use Lambda expression you need to either create your own functional interface or use Java functional interface for operation that require two integer and return as value. IntBinaryOperator

Using user defined functional interface

interface TwoArgInterface {

    public int operation(int a, int b);
}

public class MyClass {

    public static void main(String javalatte[]) {
        // this is lambda expression
        TwoArgInterface plusOperation = (a, b) -> a + b;
        System.out.println("Sum of 10,34 : " + plusOperation.operation(10, 34));

    }
}

Using Java functional interface

import java.util.function.IntBinaryOperator;

public class MyClass1 {

    static void main(String javalatte[]) {
        // this is lambda expression
        IntBinaryOperator plusOperation = (a, b) -> a + b;
        System.out.println("Sum of 10,34 : " + plusOperation.applyAsInt(10, 34));

    }
}
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › lambdaexpressions.html
Lambda Expressions (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
You can use a standard functional ... interface. It's a functional interface because it contains only one abstract method. This method takes one parameter and returns a boolean value....
🌐
W3Schools
w3schools.com › java › java_lambda.asp
Java Lambda Expressions
Lambda Expressions were added in Java 8. A lambda expression is a short block of code that takes in parameters and returns a value. Lambdas look similar to methods, but they do not need a name, and they can be written right inside a method body.
🌐
Programiz
programiz.com › java-programming › examples › lambda-expression-as-method-parameter
Java Program to pass lambda expression as a method argument
import java.util.ArrayList; class Main { public static void main(String[] args) { // create an ArrayList ArrayList<String> languages = new ArrayList<>(); // add elements to the ArrayList languages.add("java"); languages.add("swift"); languages.add("python"); System.out.println("ArrayList: " + languages); // pass lambda expression as parameter to replaceAll() method languages.replaceAll(e -> e.toUpperCase()); System.out.println("Updated ArrayList: " + languages); } } Output ·
🌐
Baeldung
baeldung.com › home › java › lambda expressions and functional interfaces: tips and best practices
Lambda Expressions and Functional Interfaces: Tips and Best Practices | Baeldung
December 16, 2023 - Functional interfaces, which are gathered in the java.util.function package, satisfy most developers’ needs in providing target types for lambda expressions and method references. Each of these interfaces is general and abstract, making them easy to adapt to almost any lambda expression. Developers should explore this package before creating new functional interfaces. ... In addition, we have a method add() in some class UseFoo, which takes this interface as a parameter...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lambda-expressions-parameters
Java - Lambda Expressions Parameters - GeeksforGeeks
July 23, 2025 - One example is java.lang.Runnable. Lambda expressions implement only one abstract function and therefore implement functional interfaces. Predicate interface is an example of a functional interface that has only one abstract method called test(). ... The above is a functional interface that has one abstract method test receiving only one parameter of type T and returns a Boolean value.
🌐
Jenkov
jenkov.com › tutorials › java › lambda-expressions.html
Java Lambda Expressions
From Java 11 var can also be used for lambda parameter types. Here is an example of using the Java var keyword as parameter types in a lambda expression: Function<String, String> toLowerCase = (var input) -> input.toLowerCase();
🌐
TutorialsPoint
tutorialspoint.com › how-to-pass-a-lambda-expression-as-a-method-parameter-in-java
How to pass a lambda expression as a method parameter in Java?
interface Algebra { int operate(int a, int b); } enum Operation { ADD, SUB, MUL, DIV } public class LambdaMethodArgTest { public static void main(String[] args) { print((a, b) -> a + b, Operation.ADD); print((a, b) -> a - b, Operation.SUB); print((a, b) -> a * b, Operation.MUL); print((a, b) -> a / b, Operation.DIV); } static void print(Algebra alg, Operation op) { switch (op) { case ADD: System.out.println("The addition of a and b is: " + alg.operate(40, 20)); break; case SUB: System.out.println("The subtraction of a and b is: " + alg.operate(40, 20)); break; case MUL: System.out.println("The multiplication of a and b is: " + alg.operate(40, 20)); break; case DIV: System.out.println("The division of a and b is: " + alg.operate(40, 20)); break; default: throw new AssertionError(); } } }
Find elsewhere
🌐
Bestprog
bestprog.net › en › 2020 › 12 › 15 › java-passing-a-lambda-expression-to-a-method-as-a-parameter-examples
Java. Passing a lambda expression to a method as a parameter. Examples | BestProg
December 15, 2020 - A lambda expression can be passed to a method as an argument. The declaration of such a method must contain a reference to the corresponding functional interface. This reference is obtained as a parameter of the method that processes the lambda expression.
🌐
Rice
clear.rice.edu › comp310 › JavaResources › lambdas.html
Lambda Functions
This associated interface is related to the lambda function only in that it is the "target" type to which the lambda is being assigned, either by variable assignment or by passing the lambda as an input parameter or output parameter of a method which is typed to that interface.
🌐
Netjstech
netjstech.com › 2015 › 06 › lambda-expression-as-method-parameter-java-8.html
Java Lambda Expression as Method Parameter | Tech Tutorials
December 22, 2020 - Lambda expression in Java as a method parameter. To pass a lambda expression as method argument the type of the parameter, which receives the lambda expression as an argument, must be of functional interface type.
🌐
Developer.com
developer.com › dzone › coding › languages › java lambda: method reference
Working with Lambda Expressions in Java
December 21, 2021 - The following program demonstrates the static method reference. First, it declares a functional interface called Intpredicate that has a method called check(). This method has an int parameter and returns a boolean value. Thus, it can can be used to test an integer value against some conditions.
🌐
Stack Abuse
stackabuse.com › lambda-expressions-in-java
Lambda Expressions in Java
May 8, 2019 - The implementation is just a bit different. Let's take the example of our String sorting lambda: ... Parameters are the same as function parameters, those are values passed to a lambda function for it to do something with.
🌐
Medium
medium.com › @kumar.atul.2122 › java-8-lambda-functional-interface-method-reference-stream-api-and-optional-class-f685143635fb
Java 8: lambda, functional interface, method reference, Stream API and Optional class | by Atul Kumar | Medium
February 19, 2023 - In this example, we define a lambda function that takes an integer as input and returns the square of that integer. We then create an instance of the Function interface, which is a functional interface that takes one input parameter and returns one output parameter.
🌐
CodingNomads
codingnomads.com › java-lambda-expressions
Java Lambda Expressions
Here's the syntax for creating a lambda expression in Java, where you can substitute the variables starting with your_ with your values and add as many parameters as you'd like.
🌐
Medium
gelopfalcon.medium.com › best-practices-when-you-use-lambda-expressions-in-java-f51e96d44b25
Best practices when you use Lambda expressions in Java | by Gerardo Lopez Falcón | Medium
June 25, 2018 - Foo foo = parameter -> { String result = "Something " + parameter;//many lines of codereturn result;}; However, please don’t use this “one-line lambda” rule as dogma. If you have two or three lines in lambda’s definition, it may not be valuable to extract that code into another method. In this post, we saw some best practices and pitfalls in Java 8’s lambda expressions and functional interfaces.
🌐
Javatpoint
javatpoint.com › java-lambda-expressions
Java Lambda Expressions
October 16, 2016 - Java Java provides a new feature called method reference in Java 8. Method reference is used to refer method of functional interface. It is compact and easy form of lambda expression.
🌐
Scaler
scaler.com › home › topics › java › lambda expression in java
Lambda Expression in Java | Scaler Topics
May 4, 2023 - Lambda expression in java is an anonymous (no name) function that does not need to define the data type of input parameters and does not need to have a return type. Lambda expression in java implements the functional interface and it can be ...
🌐
DZone
dzone.com › coding › java › java 8: lambda functions—usage and examples
Java 8: Lambda Functions—Usage and Examples
May 4, 2016 - LambdaParameters are parameters to the lambda function passed within opening parenthesis "(" and closing parenthesis ")". When more than one parameter is passed, they are separated by commas.
🌐
javathinking
javathinking.com › blog › how-do-i-define-a-method-which-takes-a-lambda-as-a-parameter-in-java-8
How to Define a Method That Takes a Lambda as a Parameter in Java 8: Syntax Explained — javathinking.com
Java 8 introduced a set of pre-defined functional interfaces in the java.util.function package to cover common use cases. These eliminate the need to create custom interfaces for simple scenarios. Some key ones include: To define a method that accepts a lambda as a parameter, the parameter’s type must be a functional interface.