0% found this document useful (0 votes)
4 views

Lambda Expression

Uploaded by

vishal46k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lambda Expression

Uploaded by

vishal46k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lambda Expression

Lambda is an anonymous function with no name, no modifier and no return type

E.g.:

public void m(){

System.out.println(“Hello World”);

Its equivalent lambda expression is ( ) -> {System.out.println(“Hello World”);};

Benefits

1. It reduces lines of code


2. Sequential and parallel execution by passing behaviour as an argument in methods
3. To call APIs very effectively
4. To write more readable, maintainable and concise code

E.g.:

public int sum(int a, int b) {

return (a+b);

Its equivalent lambda expression (int a, int b) -> {

return (a+b);

};

Rules

1. If the body of lambda contains only one statement, then curly braces are optional
2. Java compiler infer the type of variables passed in arguments, hence type is optional

So now equivalent expression is (a,b) -> return(a+b);

e.g. public int getLength(String str) {

return str.length();

So its equivalent is (str) -> str.length();

Functional Interface

If the interface contains only one abstract method then it is a functional interface.

e.g. Runnable, Callable, Comparable are functional interfaces


Advise to apply annotation @FunctionalInterface before a functional interface to ensure that it is
having 01 abstract method.

Lambda expression is used to implement functional interface in very simple manner.

e.g.

Approach-1

Functional interface

public interface MyInter {

public abstract void sayHello();

Now create a separate class and implement it

public class MyInterImpl implements MyInter{

@Override

public void sayHello() {

System.out.println(“Hello”);

public class Main {

public static void main(String args[]){

MyInter myInter=new MyInterImpl(); //1

myInter.sayHello();

Approach-2 Anonymous class for implementing interface

public class Main {

public static void main(String args[]) {

MyInter myInter1=new MyInter() {

@Override

public void sayHello(){

System.out.println(“First Anonymous class”); }

};
myInter1.sayHello(); //Output is “First Anonymous class”

//so in case functional interface is having several differing implementations then using approach-1
//several classes have to defined to implement several differing implementations but using
//approach-2 i.e. anonymous class the method follows

MyInter myInter2=new MyInter() {

@Override

public void sayHello(){

System.out.println(“Second Anonymous class”); }

};

myInter2.sayHello();//Output is “Second Anonymous class”

} //end of method main

} //end of class Main

Approach-3 using Lambda expression

public class Main {

public static void main(String args[]) {

MyInter myInter1= ()-> {

System.out.println(“First Implementation using Lambda”);

};

myInter1.sayHello();//Output “First Implementation using Lambda

MyInter myInter2= ()-> {

System.out.println(“Second Implementation using Lambda”);

};

myInter2.sayHello();//Output “Second Implementation using Lambda

} //end of main method

}// end of class Main

In above 03 approaches approach-3 is simplest, min. lines of code and clear way of implementing the
functional interface.

You might also like