Thursday, 5 February 2015

Lambda Expression in Java

Let's start with lambda expression. What is it? and how it works?
I googled lots of posts and YouTube videos till now to understand lambda expression but I found it
difficult to understand. Because I haven't used any functional language before.So I decided to write
blog to help people like me.
Don't go by name. Its same old java programing with bit different flavor. It just reduces the words
required to write a function or call function or write anonymous class.
Lambda expression syntax:-
 
Syntax :
1. (int a, int b) -> {  return a + b; }
     1. function with two parameter
     2. (a,b) -> {  return a + b; } works same.

2. () -> System.out.println("Hello World");
     1. function without parameter   
                     
3. (String s) -> { System.out.println(s); }
     1. Function with one parameter

4. (s)->System.out.println(s);
     1. no need to specify type of parameter
     2. no need of curly braces for single statement

5. () -> 42
6. () -> { return 3.1415 };

I am sure some of the programmer will try to write hello world program by using lambda expression
syntax after checking out the syntax.
like this:-
 
Class Test()
{
public static void main(String[] args)
{
()->System.out.println(“Hello world”);
}
}
 
I don't know about you guys. but I did. And found this
->we can use lambda expression only with Functional interfaces.

Q. Now what is functional interface and how it is important for lambda expression
Functional interface:- Functional interface is an interface with a single abstract method.
Ex.
 
public interface FunctionalInterface
{
public void show();
} 
As you can see the above interface has only one method so it is functional interface.
now try to write program “hello world”
here is old methods before lambda expression:-
 
public class TestFunInter
{
public static void main(String[] args)
{
FunctionalInterface f=new FunctionalInterface() {
               public void show()
  {
System.out.println("hello world");
}
};
   f.show();
} 

OUTPUT:-hello world

And
 
public class TestFunInter
{
public static void main(String[] args)
{
   new FunctionalInterface() {
   @Override
   public void show()
{
   System.out.println("hello world");
}}.show();
   }
}

OUTPUT:-hello world
 
 

now check it out in lambda way

 
public class TestFunInter
{
public static void main(String[] args)
{
FunctionalInterface f=()->System.out.println("hello world");
                  f.show();
}
}

OUTPUT:-hello world

now compare all the ways of writing same code, you can get the proper idea what the lambda is
doing for you.now some of you have started imagining and some have hammered by new question like
“It needs a functional interface to work". Means whenever we want to do anything by using lambda expression
we need to create functional interface first ”.who are in imagination please don't come out of it, let me take hammer
away first.
API developers have already provided many Functional interfaces needed for general use. And this
interfaces are located in the package java.util.function.
you can check out this link for further details.
Let's check out the java.util.function package.
example:-
 
import java.util.function.*;
public class Test
{
    public static void main(String[] args)
{
BiConsumer<String, String> bi = (a, b) -> System.out.println(a + b);
bi.accept("shiv ", "----------");
   }
}
OUTPUT:-shiv----------

you can see in above example .
1.import -> we have imported function package
2.In main method we have created reference variable of type interface with two generic types.
3.this interface has one abstract method which accepts two parameters. and returns nothing.
you can check detail here.
 
=>Lambda expression with Threads
Note:-we all know that “Runnable” interface has only one method “run()”.
so it is also a “Functional interface” and can be used with lambda expression.

Example 1:- Use of thread type 1. 

Runnable r = new Runnable()
{
public void run() 

System.out.println("I am runnable thread");
}
};
Thread t = new Thread(r);
t.start();

Let's do the same using lambda expression 

Runnable r = () -> System.out.println("lambda expression");
Thread t = new Thread(r);
t.start();

Example 2:- Use of thread type 2. 

new Thread(new Runnable(){
public void run()
{
  System.out.println("thread");
}
                                               }).start();

same by lambda. 

new Thread(()->System.out.println("lambda thread")).start();

Just compare the standard and lambda way. and you can find the things lambda made easier.
what lambda expression can do for us is:-
->Define anonymous function
->Can be assigned  to Variable
->Can be Passed to functions
->Can be returned from Functions