Lambda Expression

Lambda expression is included in java SE 8.
The Lambda expression is used to provide the implementation of an interface which has functional interface.

Functional interface

An Interface containing only one function are known as functional interface.
You can annotate that interface with @FunctionalInterface to restrict that interface
from adding more than one method.

public interface eye{
public void onlyOneMethod();
}
@FunctionalInterface
public interface eye{
public void onlyOneMethod();
}

Here both interface will work in lambda expression because both contain only one abstract method.
But annotating the interface with @FunctionalInterface will restrict from changing the interface from adding
any other method. This will help to preserve the FunctionalInterface behavior.

@FunctionalInterface
public interface eye{
public void onlyOneMethod();
public void secondMethod();
}

This will give compile time error at compile time
eye is not a functional interface
multiple non-overriding abstract methods found in interface eye

interface eye{
void onlyOneMethod();
void secondMethod();
}

public class lam{
public static void main(String[] args){
eye p = ()-> System.out.println("Pankaj");
p.onlyOneMethod();
}
}

This will give error at run time
multiple non-overriding abstract methods found in interface eye


Compile time error much better than runtime error

Why lambda?

To provide the implementation of functional interface
Less boilerplate i.e less code

How to use lambda

(“Argument”, ” it can be 0”, “or more” , “if no argument just use ()”) → {body}

@FunctionalInterface
interface eye{
void onlyOneMethod();
}

public class lam{
public static void main(String[] args){
eye p = ()-> System.out.println("Fish Eye");
p.onlyOneMethod();
}
}

What happening?
in Background something similar happen when you call lambda function

@FunctionalInterface
interface eye{
void onlyOneMethod();
}

public class lam{
public static void main(String[] args){
eye p = new eye(){
public void onlyOneMethod(){
System.out.println("Fish Eye");
}
};
p.onlyOneMethod();
}
}

Without Lambda we have to write multiple line to just implement one functionalInterface method

Example of functional interface returning value but taking no argument

@FunctionalInterface
interface eye{
String onlyOneMethod();
}

public class lam{
public static void main(String[] args){
eye p = ()-> {return "Blue eye";};
System.out.println(p.onlyOneMethod());
}
}

Example of functional interface taking 2 argument

@FunctionalInterface
interface eye{
String onlyOneMethod(String a, String b);
}

public class lam{
public static void main(String[] args){
String arg1 = "Red Eye";
String arg2 = "Green Eye";
eye p = (a,b)-> {return a+b;};
System.out.println(p.onlyOneMethod(arg1,arg2));
}
}

if your functionalInterface take only one argument you can pass the argument just by
variableToPass → {};

@FunctionalInterface
interface eye{
String onlyOneMethod(String a);
}

public class lam{
public static void main(String[] args){
String a = "RED";
eye p =b -> {return "Author Eye " + b;};
System.out.println(p.onlyOneMethod(a));
}
}

more code saving

In Java lambda expression, if there is only one statement, you may or may not use return keyword. You must use return keyword when lambda expression contains multiple statements.

@FunctionalInterface
interface eye{
String onlyOneMethod();
}

public class lam{
public static void main(String[] args){
eye p = ()-> "Devil eye";
System.out.println(p.onlyOneMethod());
}
}

Useful places to reduce boilerplate

Creating Thread

public class threadLam{  
public static void main(String[] args) {

//Thread Example without lambda
Runnable r1=new Runnable(){
public void run(){
System.out.println("Thread without lambda");
}
};
Thread t1=new Thread(r1);
t1.start();
//Thread Example with lambda
Runnable r2=()->{
System.out.println("Thread with lambda");
};
Thread t2=new Thread(r2);
t2.start();
}
}

Similarly You can use it in

public void addActionListener(ActionListener l)
button.addActionListener(e-> {tf.setText("hello swing");});
Scroll to top