Monday, April 15, 2019

Lambda Expression

 LAMBDA EXPRESSION



  • Lambda Expression is the one of the most important and biggest features in the jdk 8. It provides the clear and concise way to represent one method interface using an expression. The lambda expression is used to provide the implementation of an interface which has functional interface. it also helps to simplified the development alot. If you look at history and try to find out any language improvement in java in last 2 decades, you will not be able to recall many exciting things. Only few concurrent classes, generics and if you are agree then annotations as well, are remarkable additions in java in last decade. Lambda expression break this drought and feels like a pleasant gift.
        
In this Blog post, ill going to cover lambda expression and the basic concepts examples.                                                                                                                      
                    πŸ‘‰ Lambda expression small introduction in java
                           
                         πŸ‘‰ Java 8 functional interface

                         πŸ‘‰ Lambda expression examples.



  Lambda Expression small introduction in java

  • As I said earlier, it is an special features in java 8. and most welcoming by everyone. But the question is "Well, it is the function without the name and identifier. that's it But why everyone is so excited????? "πŸ˜²πŸ€”πŸ€”πŸ€”

  • The answer lies in the benefits involves in functional programming over oop(Object-Oriented Programming). Most OOP languages evolve around objects and instances and treat only them their first class citizens. this is specially true in java, where function can't  exist outside an object. a function itself does not mean anything in java, until it related to some object or instances.


  •   But in functional programming , you can define functions, give them reference  variables and pass them as method arguments and much more. JavaScript is a good  example of this where you can pass callback methods. its very useful feature and has been lacking in java from previous decades. Now in jdk 8, we can also use these things.
           
    

Syntax :- (a,b) - >  a+b ;  // This function has two parameters which are a and b. and it returns sum of these both.


Next, Have a look about Java 8 functional Interface.

2. Functional Interface in java 8


  • Single Abstract method Interface is not a new concept. it means interfaces with only one single method. We already had many examples. from jdk 8 , they will also be referred as functional interfaces as well. jdk 8 , enforces the rule of single responsibility by making these interfaces with  new annotation :- @FunctionalInterface .
     There is a good and well known example us Runnable Interface in thread. 



If i try to add a new method in any functional interface, compiler would not allow us to do this and will throw compile time error. 




So far good. But how they related to Lambda Expressions? πŸ€”
 Lets find πŸƒ

We know that Lambda expression is anonymous function with no name and they are passed to functions as parameters. Well in java methods parameters always have a type and this type information is looked for to determine which method needs to be called in case method overloading or even simple method calling. So normally, Every lambda expression also must be convertible to some type to be accepted as method parameters. Well that type in which lambda expressions are converted , are always of functional interface type. 


Example:- 



After use the Lambda Expression, The code will be: 



We can also see that Runnable is a functional Interface which does not allow more than one method . So when you pass lambda express to constructor of Thread class , compiler tries to convert the expression equivalent code as shown in first code sample,  If compiler succeed then everything runs fine, if compiler is not able to convert expression into equivalent implementation code, it will complain. 








3. Java 8 Lambda Expression Examples



1. Iterating over a List and perform some operations




2. Create a new runnable and pass it to thread



3. Adding an event listener to a GUI component



Above are very basic examples of lambda expression in java 8.

In the conclusion, I think this post might give the basic knowledge and examples of lambda expression to construct. Feel free to share your feedback and comments below.

Thank you for being here and keep reading..... πŸ˜ƒπŸ‘



4 comments:

Webservices — C# πŸ‘©πŸ»‍πŸ’»

  Webservices — C# πŸ‘©πŸ»‍πŸ’» There are a lot of definitions defined about web services. In simple terms, The Web service is any software syste...