Lamda Expression in java:
Lamda Expression is the most important feature of java came in java 1.8 version.It makes bigger difference between java coding because we can perform various operation in just a Second without lengthy code ,it makes java developers life very easy.
What is lamda Expression:
Lamda expression is a method without having any name,return type and modifire in java programming.
There is a universal rule behind the lamda expression .
We can use lamda expressions within the functional interface only.without functional interface we can not use lamda expression in java .If we are trying to use than we will get functional interface.
What is functional interface in java:
The interface which contains only single abstract method are called functional interface it may be predefined interface or it may be user defined interface. like comparable interface ,comprator interface ,Runnable interface etc.
We can use functional interface in multi threading and collection framework mainly and makes code as well as simple and short.
We can check an interface is a functional interface or not by using @FunctionalInterface annotation.
Example: let's check it is a functional interface or not.
@FunctionalInterface
interface I
{
public void m1();
}
class Test implements I
{
public void m1()
{
System.out.println("Demo of functional interface");
}
}
class Demo
{
public static void main(String[] args)
{
Test t=new Test();
t.m1();
}
}
Output:
Demo of functional interface
If we take two abstract methods in the interface then it's not a functional interface and we will get compile time error, let's check.
@FunctionalInterface
interface I
{
public void m1();
public void m2();
}
class Test implements I
{
public void m1()
{
System.out.println("Demo of functional interface");
}
}
class Demo
{
public static void main(String[] args)
{
Test t=new Test();
t.m1();
}
}
Compile time error:
patasi.java:1: error: Unexpected @FunctionalInterface annotation
@FunctionalInterface
Lamda Expression Example:
Example 1: Display Hello on the console with the lamda expression and without lamda Expression.
Without lamda Expression:
# Let's check the code without lamda expression
interface I
{
public void m1();
}
class Test implements I
{
public void m1()
{
System.out.println("Hello");
}
}
class Demo
{
public static void main(String[] args)
{
Test t=new Test();
t.m1();
}
}
Output:
Hello
With lamda Expression:
# Let's check the code with lamda expression.
interface I
{
public void m1();
}
class Demo
{
public static void main(String[] args)
{
I i=()->System.out.println("hello");
i.m1();
}
}
Example 2: Write a program to add two numbers in java with the help of lamda expression.
interface I
{
public void sum(int a,int b);
}
class Demo
{
public static void main(String[] args)
{
I i=(a ,b)->System.out.println(a+b);
i.sum(10,40);
}
}
Output:
50.
Write a program to define square of a number by using lamda expression.
interface I
{
public int square(int n);
}
class Demo
{
public static void main(String[] args)
{
I i=n->n*n;
System.out.println(" the square of 10 is"+i.square(10));
}
}
Output:
the square of 10 is100
In the lamda expression ,if there is only one argument then parenthesis are optional ,data type is optional and if there is only one statement then {} are also optional.