The lambda expression for Java 1. What is a lambda expression
Simply put, a lambda expression is an anonymous method. Lambda expressions allow programmers to use more concise code, but they also make the code less readable.
A lambda expression is also called an anonymous method or a closure.
2. Compare with Anonymous inner class
Lambda is an anonymous method, this time we will think of anonymous inner class, let us recall the use of anonymous internal classes, such as the following code is the use of anonymous inner class implementation of a thread.
publicclass Test { publicstaticvoidmain(String[] args) { new Thread(new Runnable() { @Override publicvoidrun() { System.out.println("线程:" + Thread.currentThread().getName()); } }); t.start(); }}
Our general practice is to write an implementation class for the Runnable interface, and then the new implementation class is passed to the constructor of thread. As follows:
publicclass Test { publicstaticvoidmain(String[] args) { newMyThread(); new Thread(myThread); t.start(); } staticclassimplements Runnable { @Override publicvoidrun() { System.out.println("线程:" + Thread.currentThread().getName()); } } }
You can see that the implementation class of the new Runnable interface is omitted by using the anonymous inner class.
3. Using lambda expressions
The above uses the notation of an anonymous inner class, if you use a lambda expression to write the following:
publicclass Test { publicstaticvoidmain(String[] args) { new Thread(() -> { System.out.println("线程:" + Thread.currentThread().getName()); }); t.start(); }}
There is a problem, if there are multiple methods in the interface, then how does the lambda expression know which method to implement? Let's test it by code:
package Com.wangjun.othersOfJava; public class lambdatest {public static void main (string[] args) {Animal a = () {//compile Error: The target type of this Expressio n must be a functional interface System. out . println ( "dog Meal" ); }; A.eat (); } interface Animal {public void eat (); public void duty (); }}
Can see compile error, this refers to a functional interface, is the function interface. a functional interface is an interface that has only one abstract method . Thus, it is not difficult to understand that the original lambda expression only supports functional interfaces.
4. Several ways lambda expressions are used
Package Com.wangjun.othersOfJava; Public classlambdatest { Public Static void Main(string[] args) {//Belt typeAnimal a1 = (String str), {System. out.println("The Dog Eats:"+ str); };//without typeAnimal A2 = (str), {System. out.println("The Dog Eats:"+ str); };//without bracketsAnimal a3 = str, {System. out.println("The Dog Eats:"+ str); };//without curly bracesAnimal a4 = str--System. out.println("The Dog Eats:"+ str); A1.Eat("Ham sausage"); A2.Eat("Beef"); A3.Eat("Noodles"); A4.Eat("Rice");//return using returnperson P1 = (), {return "Teacher's duty: Educate and Educate!"; };//Direct returnperson P2 = ()"Doctor's Duty: to heal the sick!"; System. out.println(P1.Duty()); System. out.println(P2.Duty()); }//No return value InterfaceAnimal { Public void Eat(String str); }//has a return value Interfaceperson { PublicStringDuty(); }}
4. Double colon expression for Java
The use of double colons in JDK8 is to pass the method as a parameter to the inside of the stream, so that each element of the stream is passed into the method and executed. The following is an explanation of how double colons and lambda expressions are used differently by traversing a list.
Package Com.wangjun.othersOfJava;import java.util.Arrays;import java.util.List;import Java.util.function.Consumer; Public classlambdatest { Public Static void Printstr(String str) {System. out.println(str); } Public Static void Main(string[] args) {list<string> List = Arrays.aslist("AAA","BBB","CCC");//1. The usual way to traverse for(String str:list) {lambdatest.Printstr(str); }//2. Using lambda expressions to traverseList.ForEach(str, {lambdatest.Printstr(str); });//3. Use:: TraverseList.ForEach(lambdatest::p rintstr);//The following method is equivalent to the above, using functional programmingConsumer<string> Methodparam = lambdatest::p rintstr;//Method parametersList.ForEach(X-Methodparam.Accept(x));//Method Execution Accept}}
Lambda Expressions for Java