Saturday, 8 September 2018

Use of Lamda Expression and Functional Interface in JAVA 8

In this blog, we are going to discuss one of the most important features of JAVA 8 which is Lamda Expression and Functional Interface.

As per java doc, "Lamda Expression enables Functional Interface" that means Lamda Expression allows us to write Functional Programming.

Please note that Lamda Expression takes or accepts only those interfaces which are having only one abstract method.

So What is Functional Interface?

A functional interface is an interface which is having only one abstract method. For example, we can consider as

  • java.util.Comparator interface
  • Java.lang.Runnable interface
We have created one customize Functional Interface MathHelper which is having only one method as sum(int val1, int val2).



Please note, annotation @FunctionalInterface is used to tell us that this interface will be used as Functional Interface.

Now what will happen if we add one more method multiply(int val1, int val2) like below,






Well, above scenario will not work here, compiler will give us an error as Functional Interface can not contain more that one abstract method. To overcome this problem, we can have a number of default methods like below:












What is Lamda Expression?

We are familiar with Functional Interface now but question is how can we use this functional interface in our code.

Well, the answer is Lamda Expression. Lamda expression is introduced in Java 8 and it is an Instance of Functional Interface.

Let us think that if Lamda Expression would not have introduced, then how can we implement the MathHelper Interface in Java 7. So we need to have One class lets say MathHelperImpl which implements MathHelper interface like below.














So does not it look like little lengthy because just to achieve the sum() function, we have to implement the Interface and have to Instantiate the class.

But question is why do we need to create the object? Can't we do this as a function without implementing or creating a new Object.

We can write a block of code to a variable and where ever this variable goes, this block of code also goes with that. Lets see below example,

String country= "India" // here india is assigned to a variable called country.

Double piVal="3.14" // here 3.14 is assigned to a variable called piVal.

As per above example, where ever we will use country, piVal variables in our code its value also passes with it.

If we can write a block of function like below where entire function is assigned to a variable then we can solve the above problem.

lamdaVal={
         .................. our functional code comes here............
         ..........................
                   }
So let us write the sum() method like below:
lamdaVal=public int sum(int val1, int val2) {
        return val1 +val2;}
    }

But we can get rid of couple of things from the above block like:
public - This is really not required while writing a block of code because we are writing this block inside some class or method only.
int- We don't need to mention return type as compiler can understand the return type by seeing the functional interface.
sum - For a block of code, we don't need to mention any name just like anonymous method. 

So finally the above block will look like:

lamdaVal=(int val1, int val2) {
        return val1 +val2;
    }
Even we can more specify it as below
lamdaVal=(val1, val2) {
        return val1 +val2;
    }
So the syntax is pretty much same for the Lamda Expression except one addition which is:
lamdaVal=(val1, val2) -> { val1 +val2 ;
    }
If we have only one line of code in that case we can re write the above the code as 
lamdaVal=(val1, val2) -> val1 +val2 ;

So finally we can re write our above Java 7 code using Lamda Expression in Java 8 as below:



    







Complete Example of Lamda && Functional Interface:





















Couple of things we can see from the above Lamda code...

1. We don't implement the MathHelper Interface here rather than we are using it as a functional way.

2. Using Lamda Expression we can write very less code which can not be done in Java 7.

That's all for Java 8 Functional Interface and Lamda Expression. I keep it as simple as I can. Hope you like it. I would love to see your comment and feedback.



Use of Lamda Expression and Functional Interface in JAVA 8

In this blog, we are going to discuss one of the most important features of JAVA 8 which is Lamda Expression and Functional Interface. A...