Java Lambda Expressions

Java Lambda Expressions


Lambda Expressions In Java
A lambda expression is, essentially, an anonymous method. However, this method is not executed on its own. Instead, it is used to implement a method defined by a functional interface. Thus, a lambda expression results in a form of an anonymous class. Lambda expressions are also commonly referred to as closures. 
The lambda expression relies on a syntax element and operator that differ from what we have seen in the preceding topics. The operator sometimes referred to as the lambda operator or the arrow operator that is -
 

(argument1, argument2)   ->   expression

  • It divides a lambda expression into two parts. The left side specifies any parameters required by the lambda expression.
  • On the right side is the lambda body, which specifies the actions of the lambda expression.
  • At this point, it will be helpful to look at a few examples of lambda expressions given as follows -
Example - Let’s begin with what is probably the simplest type of lambda expression you can write.
              () -> 3.14
  • This lambda expression takes no parameters.
  • Thus the parameter list is empty.
  • It returns the constant value 3.14.
  • The return type is inferred to be double. Therefore, it is similar to the following method -
Example -

double myMeth() 
{ return 3.14
     }
Example - 
Of course, the method defined by a lambda expression does not have a name. A slightly more interesting lambda expression is shown here - 
() -> Math.random() * 10
This lambda expression obtains a pseudo-random value from Math.random( ), multiplies it by 10, and returns the result. It, too, does not require a parameter.
Example - Now we will see a full example of lambda expressions - 
// Demonstrate two simple lambda expressions.
//A functional interface.
interface MyValue {
double getValue();
}
// Another functional interface.
interface My Pa r amVa 1 ue {
double getValue(double v);
}
Functional interfaces
class LambdaDemo {
public static void main(String args[])
{
MyValue myVal; // declare an interface reference
// Here, the lambda expression is simply a constant expression.
// When it is assigned to myVal, a class instance is
// constructed in which the lambda expression implements
// the getValue() method in MyValue.
myVal = () -> 98.6; ----------------------------A simple lambda expression
// Call getValue(), which is provided by the previously assigned
// lambda expression.
System.out.printIn("A constant value: " + myVal.getValue());
// Now, create a parameterized lambda expression and assign it to
// a MyParamValue reference. This lambda expression returns
// the reciprocal of its argument.
MyParamValue myPval = (n) -> 1.0 / n; <----------------A lambda expression that
has a parameter
// Call getValue(v) through the myPval reference.
System.out.println("Reciprocal of 4 is " + myPval.getValue(4.0));
System.out.printin("Reciprocal of 8 is " + myPval.getValue(8.0));
// A lambda expression must be compatible with the method
// defined by the functional interface. Therefore, these won't work:
// myVal = () -> "three"; // Error! String not compatible with double!
// myPval = () -> Math.random(); // Error! Parameter required!
 
}}
Output - sample output is here as follows -
A constant value: 98.6
Reciprocal of 4 is 0.25
Reciprocal of 8 is 0.125