Java 8 Functional Interfaces and Lambda Expressions
RITESH SINGH
| Certified Software Architect in Microservices| Springboot| Java8 | REST| Reactive Programming | Data Structure & Algorithm | AWS EC2, ECS | Design Pattern
Functional interface is just an interface with one abstract method and any number of default and static methods. To avoid accidental addition of abstract method in functional interfaces , we use @FunctionalInterface annotation from java.util.function.Function. @FunctionalInterface annotation ensures that the interface can’t have more than one abstract method. If we define more than one abstract method in this interface, then the compiler shows an error ‘Invalid '@FunctionalInterface' annotation.’ For example Functional interfaces in java are Runnable and Comparable .
Let's understand Lambda expression : A method without name is known as anonymous method. Lambda expression is an anonymous method which use to implement abstract method of Functional interface.
While defining lambda expression there is used an operator called Arrow '->' operator . Take a look of below snippet code :
(name) -> System.out.println(name);
Now ,Let's understand above snippet code :
Left side of Arrow(->) operator defines the parameters of abstract method of functional interface if no parameters than you can leave empty and Right side of Arrow(->) operator is the action of lambda expression or in other words body definition of abstract method of Functional interface and now you are thinking about return type of lambda expression , whatever you return from lambda expression that will become it's return type but in above example no return type.
Now Let's Take a look for thread creation without lambda and using lambda : First we are going to create thread without lambda using class :
class MyThread implements Runnable{
public void run(){
System.out.println("My Thread started!");
}
}
public class MainClass{
public MyThread myThread = new MyThread();
public static void main(String... args){
MainClass obj = new MainClass();
Thread t = new Thread(obj.myThread);
t.start();
}
}
Now, Let's understand above code. Here we created a class named 'MyThread' and implements a Functional interface Runnable which contains only one abstract method named 'run' then we have to override run method in 'MyThread' class. Now we create 'myThread' class's instance and pass to Thread class constructors and start Thread.
Now Let's do the same thing using Anonymous class :
public class MainClass{
public Runnable myThread = new Runnable(){
public void run(){
System.out.println("My Thread started!");
} };
public static void main(String... args){
MainClass obj = new MainClass();
Thread t = new Thread(obj.myThread);
t.start();
}
}
Now you can see above, here we reduce some code and we directly provide implementation to Runnable using anonymous class.
Now Let's understand how Lambda expression does the same thing :
public class MainClass{
public Runnable myThread =()
->System.out.println("My Thread started!");
public static void main(String... args){
MainClass obj = new MainClass();
Thread t = new Thread(obj .myThread );
t.start();
}
}
Now you can see we reduce more code again. Let's understand Lambda expression here Arrow(->) operator Left side is blank because abstract method 'public void run()' of Runnable functional interface has no parameters and Right side of Arrow(->) operator is definitions of abstract method 'public void run()' of Runnable functional interface.
Let's see the Some example for parameterizes abstract methods of Functional interface : I am going to create one Functional interface with two Integer param and return integer value :
import java.util.function.Function;
@FunctionalInterface
interface AddNumber{
Public void add(int firstNumber, int secondNumber);
}
Now, I am going to implement this Functional Interface using Lambda:
Public class MainClass{
public static AddNumber sum = (int number1, int number2) ->
System.out.println("sum ="+(a+b));
public static void main(String... args){
MainClass.sum.add(10,30);
}
}
Output :
sum =40
Now, Let's have a look of return value from Lambda Expression : I am going to create a functional interface which abstract method returns integer value.
import java.util.function.Function;
interface AddNumber{
public int add(int number1, int number2);
}
Now, here we go for Lambda implementation :
public class MainClass{
public static AddNumber sum = (int number1, int number2) -> {
return a+b;
}
public static void main(String... args){
int sum = MainClass.sum.add(20,10);
System.out.println(sum);
}
}
Output
30
In above snippet code , Right side of Arrow(->) operator part known as Lambda function body enclosed with {} . In Lambda function body you can pass multiple lines of code. We can also define return type in single line statement , checkout below snippet code :
public class MainClass{
public static AddNumber sum = (int number1, int number2) ->
return a+b;
public static void main(String... args){
int sum = MainClass.sum.add(20,10);
System.out.println(sum);
}
}
Output:
30