Lambda expressions are a feature introduced in Java 8 that allow functional programming in Java. They provide a concise way to represent functional interfaces (interfaces with only one abstract method) using an expression.
For example, consider the following functional interface for a single method that takes two integers as inputs and returns an integer:
@FunctionalInterface
public interface MathOperation {
int operation(int a, int b);
}
A lambda expression can be used to implement this functional interface, such as the following addition operation:
MathOperation addition = (int a, int b) -> a + b;
This method can be called with a lambda expression, such as:
int result = operate(1, 2, (a, b) -> a + b);
In this example, the lambda expression (a, b) -> a + b is passed as an argument to the operate method, which applies the operation method to 1 and 2 and returns 3.
Comments
Post a Comment