Lambda expression is similar to java method, but lambda expression does not need name and it can be implemented in the body of a method.
It is an instance of functional interface. An interface with a single abstract method is called functional interface.
The lambda expression is a short block of code which takes parameters and returns a value
parameter -> expression
//Syntax of more than one parameters
(parameter1, parameter2) -> expression
/*@FunctionalInterface*/
//It is optionalpublic interface MultipleInterface { public int multiple(int x,int y); } public class Calculate
{ public static void main (String[] args) {
//with lambdaMultipleInterface m = (x, y) -> { return x * y; } int multipleVal = m.multiple(10,30); System.out.println( "Multiple Value is "+multipleVal); } Output: Multiple Value is 300
//Importing required classesimport java.util.*;
import java.io.*;
import java.util.stream.*;
public
class LambdaExample {
public static void main (String args[]){
Integer
[] numbers = { 100, 20, 13, 440, 5, 16, 7, 28, 2, 11, 1 };
Arrays.asList(numbers) .forEach((i) -> { System.out.println(); } );
}
}
Output: 100 20 13 440 5 16 7 28 2 11 1
//Importing required classesimport java.util.*;
import java.io.*;
import java.util.stream.*;
public
class LambdaExample {
public static void main (String args[]){
Integer
[] numbers = {100,20,13,440,5,16,7,28,2,11,1};
Consumer
consumerMethod = (i) -> { System.out.println(i); }; Arrays.asList(numbers) .forEach( consumerMethod );
}
}
Output: 100 20 13 440 5 16 7 28 2 11 1