Lambda expression
To tell you the truth, when I first saw this word, it was very obscure. How to read it back and forth was very strange. Later, what you understand, of course, was to search it. Hahaha, it turned out to be that λ
It allows functions to be passed as arguments, ← to hit the blackboard! It solves the problem of whether a function can exist independently;
Also known as closure, it is the most important new feature to promote Java 8 release;
Java is a kind of approach to functional programming (functional programming, can connect methods to avoid multiple lines of code is not easy to read) = > java 8 supports functional programming
Grammatical format
(parameters) -> expression
or
(parameters) -> { statements; }
Can only be used when there is a functional interface
Four core functional interfaces:
- Consumer: consumer interface
void accept(T t); - Supplier: supply interface
T get(); - Function: function interface
R apply(T t); - Predicate: assertion interface
boolean test(T t);
Chestnut
Bronze:
1. No parameter is required, and the return value is 9 / / if the parameter list is empty, just keep (); () -> 9
2. Receive multiplication (numeric type) //If there is only one parameter, () can be omitted, only the name of the parameter is required; x -> 3 * x
3. Receive subtraction (numeric type) (x, y) -> x – y
4. Receive2A certain type, Do arithmetic (int x, int y) -> x + y
5. Receive one string object,And print,Does not return any value (looks like it doesvoid) (String s) -> System.out.print(s)
Specific:
package test; public class lab001 { public static void main(String[] args) { lab001 tester = new lab001(); MathOperation addition = (int a, int b) -> a + b; // Type declaration MathOperation subtraction = (a, b) -> a - b; // No type declaration. It has been declared above. The compiler can recognize MathOperation multiplication = (int a, int b) -> { return a * b; };// Return statements in braces MathOperation division = (int a, int b) -> a / b;// No braces and return statements System.out.println("2 + 3 = " + tester.operate(2, 3, addition)); System.out.println("2 - 3 = " + tester.operate(2, 3, subtraction)); System.out.println("2 x 3 = " + tester.operate(2, 3, multiplication)); System.out.println("6 / 2 = " + tester.operate(6, 2, division)); zi greetService1 = message -> System.out.println("Hello " + message);// No brackets zi greetService2 = (message) -> System.out.println("Hello " + message);// Parenthesis greetService1.sayMessage("2020"); greetService2.sayMessage("hello"); } //There must be an interface interface MathOperation { int operation(int a, int b); } interface zi { void sayMessage(String message); } private int operate(int a, int b, MathOperation mathOperation){ return mathOperation.operation(a, b); } }
6. Only outer local variables marked final can be referenced. If it is not final, it must not be modified by subsequent code (i.e. implicit with final semantics), or compilation error. And it is not allowed to declare a local variable with the same name public class test { final static String put= "Hello "; public static void main(String args[]){ GreetingService zi = message -> System.out.println(put + message); zi.sayMessage("2020"); } interface GreetingService { void sayMessage(String message); } } Output: Hello 2020
7.You can directly access the outer local variables public class test { public static void main(String args[]) { final int num = 1; Converter<Integer, String> zi = (result) -> System.out.println(String.valueOf(result+ num)); zi.convert(2); // Output 3 } public interface Converter<T1, T2> { void convert(int i); } }
The reason why it is so called is vivid. Do you have it? Please look down obliquely for details:
Today's study is here, if there is something wrong, welcome to correct! Thank you very much!
Main reference blog:
-
https://blog.csdn.net/qq_30332665/article/details/75140562 ->The history of Java
(when learning the new features of Java8 at the beginning, I went to find various materials and blogs to learn. I didn't understand what I was talking about even after I read one. I'm very ignorant. This blog helps me and I hope it can help you as well.) - https://blog.csdn.net/visant/article/details/79778967 (introduces the new features of Java 5 ~ Java 10 in detail)
- https://blog.csdn.net/wangchengming1/article/details/81701798 (very specific)
- https://blog.csdn.net/xiexiaotian11/article/details/80886944 (there's a pithy formula)
- https://blog.csdn.net/qq_29411737/article/details/80835658
- https://blog.csdn.net/codingtu/article/details/87655804 (mainly talking about Lambda, Optional, Stream)
- https://www.runoob.com/java/java8-lambda-expressions.html (Rookie)
- https://www.iteye.com/blog/lixh1986-2329376