Lambda - Nameless methods we are calling as lambda,
We heard anonymous class as nameless class, What is nameless function ?
Lambda are advanced short cut version of anonymous class.
Usecase: Convertion of Method as Nameless Method (Lambda)
public void fan() {
System.out.println("Ramesh is megastar fan");
}
As I told lambda means nameless function/method, Now I am going to remove the method name "public void fan"
() -> {
System.out.println("Ramesh is megastar fan");
}
For the single statement we don't require curly brackets {,} and then
() -> System.out.println("Ramesh is megastar fan");
2nd Ex: methods with arguments .
public void add(int a,int b) {
System.out.println(a+b);
}
As I told lambda means nameless function/method, Now I am going to remove the method name "public void add"
(int a,int b) -> {
System.out.println(a+b);
}
For the single statement we don't require curly brackets {,} and then
(int a,int b) -> System.out.println(a+b);
If the type of the parameter can be decided by compiler automatically based on the context then
we can remove types also.
(a,b) -> System.out.println(a+b);
3rd Ex: method with arguments and return type
public String str(String str) {
return str;
}
As I told lambda means nameless function/method, Now I am going to remove the method name "public String str"
(String str) -> { return str };
If the type of the parameter can be decided by compiler automatically based on the context then we can remove types also.
(str) -> { str };
(str) -> str ;
Hope you understand lambda secret, As I told lambda is advanced version of anonymous class, Now we will see the scenario.
Concept: Functional Interface.
If an interface contain only one abstract method, such type of interfaces are called functional
interfaces and the method is called functional method or single abstract method (SAM).
Ex:
1) Runnable -> It contains only run() method
2) Comparable -> It contains only compareTo() method
3) ActionListener -> It contains only actionPerformed()
4) Callable -> It contains only call() method
Inside functional interface in addition to single Abstract method (SAM) we write any number of
default and static methods.
Usecase: Convertion of Anonymous Class as Lambda (Anonymous method)
class Test {
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
public void run() {
for(int i=0; i<10; i++) {
System.out.println("Child Thread");
}
}
});
t.start();
for(int i=0; i<10; i++)
System.out.println("Main thread");
}
}
In the above example anonymous (Nameless class) ->
new Runnable() {
public void run() {
for(int i=0; i<10; i++) {
System.out.println("Child Thread");
}
}
}
Converting above one into Lambda (Nameless Function/Method)
For the nameless Function - we only focus on function or method, here function/method is run() method.
public void run() {
for(int i=0; i<10; i++) {
System.out.println("Child Thread");
}
}
Remove the function name ->
() -> {
for(int i=0; i<10; i++) {
System.out.println("Child Thread");
}
}
We successfully converted the Anonymous class into Lambda.
Few more scenarios:
interface Calculator {
public void sum(int a,int b);
}
class Demo implements Calculator {
public void sum(int a,int b) {
System.out.println("The sum:"+(a+b));
}
}
public class Test {
public static void main(String[] args) {
Calculator cal = new Demo();
cal .sum(20,5);
}
}
Convert into anonymous class approach
public class Test {
public static void main(String[] args) {
//Calculator cal = new Demo();
Calculator cal = new Calculator() {
public void sum(int a,int b) {
System.out.println((a+b));
}
};
cal .sum(20,5);
}
}
Now Converting anonymous class into anonymous function/method (Lambda approach)
public class Test {
public static void main(String[] args) {
//Calculator cal = new Demo();
Calculator cal = (a,b) -> {
System.out.println(a+b);
};
cal .sum(20,5);
}
}
Hope you understand lambda alias nameless function/method concept.