Java Lambda Expressions
Java Lambda expressions are a new feature introduced in Java 8 that allows for the creation of anonymous functions, which can be passed as arguments to methods or stored as variables. They provide a concise and expressive way of writing code, making it easier to work with collections and streams.
A Lambda expression is essentially a block of code that can be treated as an object. It consists of a set of parameters, a lambda arrow "->", and a body that can be either an expression or a block of statements.
Java Lambda Expression are capable to implement and express functional interface. That is very useful mechanism which are capable to bind multiple logics in single method. Let's seen an example.
//interface which is define only one abstract method
@FunctionalInterface
interface SimpleOperation {
int operation(int x,int y);
}
public class Testing{
public static void main(String[] args) {
//Lambda expression to implement functional
//interface abstract method
SimpleOperation add = (int x,int y)-> x+y;
SimpleOperation subtract = (int x,int y)-> x-y;
SimpleOperation multiply = (int x,int y)-> x*y;
int result = add.operation(4,1);
System.out.println(result);//5
result = subtract.operation(4,1);
System.out.println(result);//3
result = multiply.operation(4,2);
System.out.println(result);//8
}
}
5
3
8
In this example SimpleOperation is a Functional Interface. Which is allow to define single abstract method. Here operation() is abstract method.

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment