My articles are open to everyone; non-member readers can read the full article by clicking this link.
1.What is Lambda?
We know that for a Java variable, we can assign a “value” to it, and you can do something with it.
Integer a = 1;
String s = "Hello";
System.out.println(s + a);
What should you do if you want to assign a “piece of code” to a Java variable?
For example, I want to assign the block of code on the right to a Java variable called codeBlock:
Before Java 8, this was not possible. But after the advent of Java 8, it can be done using the Lambda feature. The intuitive expression is as follows:
codeBlock = public void doSomething(String s) {
System.out.println(s);
}
Of course, this is not a very concise way of writing. So, in order to make this assignment operation more elegant, we can remove some useless declarations.
In this way, we have successfully assigned a “piece of code” to a variable very…