Introduction to Java 8 in Layman's terms - Lambda expression & Functional interfaces basics

What is Lambda expression ?

To put it in simple terms Lambda expression is a short hand form of functions, more specifically anonymous functions.

Anonymous functions are something that is not associated to a class . That is exactly what Lambda expressions are also.

How do we write a Lambda expression ?

Well there are 3 main sections of an :

(     )                  ->             { }

 Input             Arrow          Body

parameters

Below let us look at individual sections of a Lambda expression.

Input parameters - This section is similar to input parameter section of a method where we provide input parameters. This section usually is surrounded by () brackets. It can have zero or more parameters .

Arrow or Arrow operator - Arrow is something that indicates the flow towards the body of the Lambda expression. IT pretty much just denotes the end of input parameters section and beginning of the body.

Body - Body is where we provide what operation the Lambda expression has to perform. It can contain one or more statements . If there are more than one statement then they have to be separated by a comma.

Usually the body starts with { brace and ends with } brace . But of the body has only one statement we can remove these braces and write the expression as below .

(a,b) ->return a+b;

Even better if the body has only one return statement then we can remove the return keyword also . It will automatically return the result.

(a,b) -> a+b;

Why to we need Lambda expression or what is it used for ?

Well that is where Functional interfaces comes into picture .

Functional interfaces are interface that have only one method. It was a new feature of Java 8. Prior to Java 8 there were few Functional interfaces like Runnable . But they are extensively used in Java 8.

Java 8 provides us with ready to use Functional interfaces like Consumer, Predicate etc.. which we will not go in deep in this section. We can also create our own functional interface by providing only one method definition and annotating the interface with @Functional annotation.

       @Functional
        interface TestFunctionalInterface {

          int add(int a,int b);

       }

There is also one more point to note here. Functional interfaces can have only one method definition but it can have default and static methods also .

           @Functional
            interface TestFunctionalInterface {

                  int add(int a,int b);

                 default void printMessage(){

                      System.out.println("What !!!!!!!!")

                }

           }

Now you might be wondering why is this guy talking about this . It is because main purpose of Lambda expression is to provide method body for the Functional interface's method .

Let's look at the example below .

                @Functional
                interface TestFunctionalInterface {

                      int add(int a,int b);

                     default void printMessage(){

                          System.out.println("What !!!!!!!!")

                    }

               }

       Class Solution {

              Public static void main(String[] args){

                 TestFunctionalInterface test = (a,b)->a+b;

                 System.out.println("Sumation of 1 +2 is "+test.add(1,2));

           }

      }

One common example we find everywhere for Lambda expression : The Comparator example .

public class ComparatorExample{

    public static void main(String[] args){

       //Java 7 way 

      Comaparator<Integer> oldComparator = new Comparator<Integer>{

         @Override
         public int compare(Integer a,Integer b){

                int result = 0;

                if(a>b){

                    result = 1;

               }else if(a<b){

                    result = -1;

               }

               return result;

         }

     }

        System.out.println("Java 7 comaprision "+ oldComparator.compare(2,4));



        // Java 8 way



      Comparator<Integer> newComparator = (a,b) -> a.compareTo(b);

     System.out.println("Java 8 comaprision "+ newComparator.compare(2,4));

   }



}

See how much code is reduced in Java 8 way . This is one of the major advantage of Lambda expression . It makes code precise and readable .

Note: Kindly correct me in any info that is wrong on above blog . I am happy to learn .

Thank you

No Comments Yet