Java 8 - Lambda Expressions

Java 8 - Lambda Expressions

learninjava
March 18, 2015 - Java 8
 

Java 8 - Lambda Expressions 

To understand Lambda Expressions, we first need to understand what a Functional Interfaces is ? Lambda Expressions treats functionality as method arguments or code as data. Very difficult definition to understand ? For Me too!! Simply said, lambda expressions simplifies the syntax when we have only one method in an interface which needs to be called. No more weird anonymous inner class syntax.
 Remember!
The lambda expressions can be specified in 3 different styles:
1. ([DataType] parameter(s)) -> statement
For example,
e -> System.out.println("Using flatMap() : " + e.size);
2. ([DataType] parameter(s)) -> { statements }
For example,
b -> {
System.out.println("Thread Name in map : " + Thread.currentThread().getName());
return b.toUpperCase();
}
3. ([DataType] parameter(s)) -> expression
For example,
(b1, b2) -> b1 + b2;
For more examples, seeStreamexamples.
The DataType is an optional field and can be ommitted.
For instance, consider the below example,
loading...
Output:

Sucessfully sent an Anonymous bird to hit the pig...
Sucessfully sent a Lambda Expression bird to hit the pig...

This example defines a functional interface and tries to access the method in it using both anonymous inner class syntax and usign Java 8 lambda expressions syntax.
Using the anonymous inner class syntax is understandable but we are creating a new class with the complete method declaration and the logic inside the method. However, in case of lambda expressions, we are doing the same using a single statement. So, why should we need to provide the entire class and method definitions when our task is to add logic only to the method in the class. Lambda expresssions does exactly this.
In the above example, we refer to the method hitThePig() by just (). The logic inside the method is provided on the right had side of -> operator. This is like saying to the compiler "call the only method in the functional interface whose implementation is provided after -> operator."
If the method accepts a parameter, say the color of the bird then the syntax slightly varies.
The same is shown with an example below:
loading...
Output:

Sucessfully sent an Anonymous bird with color RED to hit the pig...
Sucessfully sent a Lambda Expression bird with color RED to hit the pig...

Observe now that the method hitThePig(birdColor) now accepts an argument called birdColor. The rest is self-explanatory.
 Remember!
The data type of the argument birdColor in the below statement is a not mandatory requirement. That means instead of writing the following statement,
hitThePigUsingAngryBird((String birdColor) -> ...
we can simply omit the data type and modify the statement as below:
hitThePigUsingAngryBird((birdColor) ->This is perfectly legal and valid syntax.
Thats all folks !! Happy coding. If you feel this helped you, keep supporting us by   or  or  below or on the articles on social media.
 
Like us on: