Lambda Expressions introduce functional programming concepts to Java, which is a completely object-oriented and imperative programming language. Lambda expressions are the most popular feature of Java 8.

What is a Lambda Expression?

A Lambda expression is a block of code that we can pass around to execute. General behavior to define code is encapsulate inside method bodies and execute through object references as it is with this code:

This is classic OOP style of hiding method implementations from the caller. The caller simply passes a variable to the method which then does something with the value of the variable and returns another value or produces a side effect as it is in our case.

Now let us refactor the above code to use a lambda:

Why Lambda Expression?

Lambda expressions enable us to have a more compact code, easier to read and follow. The most pressing one for the Java platform is that they make it easier to distribute the processing of collections over multiple threads.

Currently, lists and sets are typically processed by client code obtaining an iterator from the collection, then using that to iterate over its elements and process each in turn. If the processing of different elements is to proceed in parallel, it is the responsibility of the client code, not the collection, to organize this.

In Java 8, the intention is instead to provide collections with methods that will take functions and use them, each in different ways, to process their elements. The advantage that this change brings is that collections can now organize their own iteration internally, transferring responsibility for parallelisation from client code into library code.

However, for client code to take advantage of this, there needs to be a simple way of providing a function to the collection methods. Currently the standard way of doing this is by means of an anonymous class implementation of the appropriate interface. But the syntax for defining anonymous inner classes is too clumsy to be practicable for this purpose.

How to use Lambda Expression?

Lambda expressions are implementation of the only abstract method of functional interface that is being implemented or instantiated anonymously.

In above example, first one is the traditional way by creating an anonymous inner class that will implement LambdaFunctional interface and Override printData().

Second one is brand new Lambda, with light syntax and no additional typing overhead.

Structure of Lambda Expression

Lambda expression syntax can be divided into three parts.

  1. Argument List :
    • A lambda expression can contain zero or more arguments.
    • You can eliminate the argument type while passing it to lambda expressions, those are inferred types. i.e. ( int a ) and ( a ) both are same.
    • More than one arguments are separated by comma (,) operator.
    • You can also eliminate “()” if there is only argument to avoid Horizontal Problem. ( arg ) -> {…} can be written as arg -> {…}
    • You can not use inferred and declared types together, Following is invalid. Example : ( int arg1, arg2 ) // This is invalid
  2. Arrow (->) token
  3. Body of a lambda expression :
    • Body can be a single expression or a statement block.
    • If a body contains only single expression than expression will be simply evaluated and returned. () -> System.out.println(“No argument”);
    • If a body is statement of block, than it will be evaluated same as a method body, that will be called and a hidden return statement at the end block that will return control to caller after execution of block. So, branching statements ( continue and break ) are illegal and return is not necessary to write. () -> { System.out.println(“Bad lambda”); break ; // this statement is not allowed here. }
Where to use Lambda Expression?

Lambda expressions can be used anywhere in Java 8 where we have a target type. In Java, we have targeted type in the following contexts

  1. Variable declarations and assignments
  2. Return statements
  3. Method or constructor arguments
It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *