This article gives credit to Luis Santiago
introduction of lambda Link to heading
Lambda expressions take advantage of parallel process capabilities of multi-core environments as seen with the support of pipeline operations on data in the Stream API.
They are anonymous methods (methods without names) used to implement a method defined by a functional interface. So the key here is that lambda and interface comes together.
functional interface Link to heading
A functional interface is an interface that contains one and only one abstract method.
The Arrow Operator Link to heading
n -> n*n
The left side specifies the parameters required by the expression
The right side is the lambda body which specifies the actions of the lambda expression
It is helpful to think -> operator as “becomes”, in this case, n becomes n*n.
put functional interface and arrow operator together Link to heading
interface NumericTest {
boolean computeTest(int n);
}
public static void main(String args[]) {
NumericTest isEven = (n) -> (n % 2) == 0;
NumericTest isNegative = (n) -> (n < 0);
// Output: false
System.out.println(isEven.computeTest(5));
// Output: true
System.out.println(isNegative.computeTest(-5));
}
Block Lambda Expressions Link to heading
There is another type of expression used when the code on the right side of the arrow operator contains more than one statement known as block lambdas
n -> {
//code here
}
Generic Functional Interfaces Link to heading
A lambda expression cannot be generic. But the functional interface associated with a lambda expression can. It is possible to write one generic interface and handle different return types like this
interface MyGeneric {
T compute(T t);
}
public static void main(String args[]){
// String version of MyGenericInteface
MyGeneric<String> reverse = (str) -> {
String result = "";
for(int i = str.length()-1; i >= 0; i--)
result += str.charAt(i);
return result;
};
// Integer version of MyGeneric
MyGeneric<Integer> factorial = (Integer i) -> {
int result = 1;
int n = 5;
for(int i=1; i <= n; i++)
result = i * result;
return result;
};
// Output: omeD adbmaL
System.out.println(reverse.compute("Lambda Demo"));
// Output: 120
System.out.println(factorial.compute(5));
}
Lambda Expressions as arguments Link to heading
One common use of lambdas is to pass them as arguments.
They can be used in any piece of code that provides a target type. I find this exciting, as it lets me pass executable code as arguments to methods.
To pass lambda expressions as parameters, just make sure the functional interface type is compatible with the required parameter.
interface MyString {
String myStringFunction(String str);
}
public static String reverseStr(MyString reverse, String str){
return reverse.myStringFunction(str);
}
public static void main (String args[]) {
// Block lambda to reverse string
MyString reverse = (str) -> {
String result = "";
for(int i = str.length()-1; i >= 0; i--)
result += str.charAt(i);
return result;
};
// Output: omeD adbmaL
System.out.println(reverseStr(reverse, "Lambda Demo"));
}