Java Lambda Expressions

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.

Java Lambda Expressions Example


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







© 2021, kalkicode.com, All rights reserved