What is lambda Expressions?

                  The lambda expression is just like the function it consist of

  • List of parameters
  • Method Body
  • return Type

Lambda Expression termed as anonymous because it does not have any name.

Lambda Expression passed as arguments to the methods.
Anatomy of Lambda expression 🙂
Yes guys you heard it right we are going to do the dissection of the Lambda Expression.
Lambda expression=Parameters+Arrow+Method Body
(Car c1,Car 2)   ->      c1.getPrice().comparTo(c2.getPrice());

Please have a look at the below code snippets where i have compared codes of Java7 and Java8

Example 1
Using Java7

Comparator<Car> byPrice=new Comparator<Car>(){

Public int compare(Car c1,Car c2){
    return c1.getPrice().compareTo(c2.getPrice());
By Lambda Expressions

Using Java8

Comparator<Car> byPrice=(Car c1,Car c2)—>c1.getPrice().compareTo(c2.getPrice());

Example 2

class A implements Runnable
public void run()
System.out.println(“I am Java7”);

By Lambda Expression

()->{System.out.println(“I am Java8”);}

Advantage of Lambda Interface:

Cleaner Code: your code is much easier to read if you use lambdas.

Leave a Reply

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