1.LAMBDA Expressions-background knowledge and related evaluations:Lambda expressions allow you to replace a functional interface with an expression.
Function programming is fully embodied in C #, Python, and JavaScript. Java until the latest Java 8 began to formally support functional programming, the most obvious improvement is the support of Lamba expression. As C # 's father Anders Hejlsberg in the main trend of programming languages, the future programming languages will gradually fuse their own features without the existence of a purely declarative language (such as previous Java) or a purely functional programming language. In the future, the declarative programming language draws on the function programming thought, the function programming language fusion declarative programming characteristic ... This is almost an inevitable trend.
The mainstream programming language in the world today incorporates a powerful closure concept, but with one exception, it is java. Over the years, the work of adding closures to the Java language seems to have made no headway.
As early as 15, the authors of the Scala language and Typesafe framework, Martin Odersky and Phillip Wadler, launched an experimental "Pizza" project, in which people began trying to incorporate closures into one of the basic features of programming languages. Although this may seem a bit too complex, the Java community probably has the idea of embracing closures in the 2008. However, due to Oracle's hasty takeover of Sun Microsystems, Java was left out, and the release of the new version of the Java language was continually delayed.
But in Java8, things have changed a lot, and the Java language has finally equipped the Java programming force with a closed-pack weapon. "Maybe this is the most important upgrade of the Java programming language ever," said Brian Goetz, Oracle's Java language Architect. He points out that the introduction of closures in Java will have a greater impact on the Java programming approach than the generic features introduced in JAVA5. "Just as generics enable developers to abstract data types, the purpose of lambda is to allow programmers to abstract program behavior." ”
The name lambda is derived from the lambda project that binds the closure to the Java programming language. What can I do with lambda and the introduction of closures? You can think of it as a way for programmers to use a piece of program code as data. A method can be defined and used in a way that defines and uses a variable, and the defined methods can be passed as parameters to other methods, as if they were an object instance or a type data. "It seems like it's nothing, but it actually has a huge impact," Goetz said. "This will fundamentally change the way we develop Java programs." ”
We waited too long, but with the release of Java8, Lambda finally became one of the formal features of the Java specification. A grammar that was initially discarded by a traditional programmer due to being overly complex will eventually become a standard technique that can be seen in every modern Java application.
2.LAMBDA Expressions-Basic syntax and usage details: Lambda expressions support code blocks as method parameters, and lambda expressions allow for the use of more concise code to create an instance of an interface with only one abstract method.
When a lambda expression creates an object in place of an anonymous inner class, the code block of the lambda expression replaces the method body that implements the abstract method, and the lambda expression is the equivalent of an anonymous method. The main function of lambda expressions is to replace the tedious syntax of anonymous inner classes.
* For example, an array of interfaces and classes like the following:
Method of handling Behavior interface public Interface command{//encapsulation processing behavior void process (int[] target);}
The class that handles the array public class processarray{//method parameter for the processed array and the process command public void process (int[] Target,command cmd) {cmd.process (target) ;}}
Print processing implementation processing interface public class Printcommand implements Command{public void process (int[] target) {for (int temp:target) System.out.println ("element of the iteration output target array:" +temp+ "\ T");}}
The sum process implements the processing interface public class AddCommand implements Command{public void process (int[] target) {int sum=0;for (int temp:target) Sum+=temp; SYSTEM.OUT.PRINTLN ("The result of the target array summation is:" +sum+ "\ T");}}
Workaround for using anonymous inner classes before JAVA8:
Use anonymous objects and anonymous inner classes to implement public class Commandtest{public static void Main (string[] args) {Processarray pa=new processarray (); int [] target={5,1,0,9,-2,10};//Create anonymous object pa.process (Target,new printcommand ());p a.process (target,new AddCommand ());// Call the method that handles the array, depending on the anonymous inner class pa.process (Target,new Command () {public void process (int[] target) {for (int temp:target) System.out.println ("element of the iteration output target array:" +temp+ "\ T");}}); Pa.process (target,new Command () {public void process (int[] target) {int sum=0;for (int temp:target) sum+=temp; System.out.print ("The result of the target array summation is:" +sum+ "\ T");}});}}
The lambda expression provided by Java8 avoids the tedious syntax of using inner classes:
Lambda expression using JAVA8 public class Commandtest2{public static void Main (string[] args) {Processarray pa=new Processarray (); int[] target={5,1,0,9,-2,10};//handles the array, depending on the inner class pa.process (target, (int[] arr)->{int sum=0;for (int Tmp:target) sum+=tmp; SYSTEM.OUT.PRINTLN ("array element and for" +sum);});}}
A lambda expression consists of 3 parts:
<1> formal parameter list. The formal parameter list allows you to omit the parameter type, and if the formal parameter list has only one parameter, the parentheses of the formal parameter list can be omitted.
<2> arrows (.).
<3> code block. If the code block has only one statement, the lambda expression allows the curly braces of the code to be omitted, and the lambda code block has only one return statement, which can omit the return keyword.
For example, a complete example below demonstrates the simplified use of lambda expressions:
Interface eatable{void taste ();} Interface flyable{void Fly (String weather); Interface addable{int Add (int a,int b);} public class lambdademo{//calls this method requires eatable object public void Eat (Eatable v) {System.out.println (v); V.taste ();} Calling this method requires the Flyable object public void Fly (Flyable v) {System.out.println (v); V.fly ("Sunny, cloudless");} Calling this method requires Addable object public void Add (addable v) • {System.out.println (v); System.out.println ("5 and 3 of the And:" +v.add (5,3));} public static void Main (string[] args) {Lambdademo ld=new lambdademo ();//lambda expression has only one statement, you can omit curly braces ld.eat (()- SYSTEM.OUT.PRINTLN ("Delicious! ")); the formal parameter list of the//LAMBDA expression has only one formal parameter, which can be omitted from the parentheses Ld.fly (weather->{system.out.println (" Weather today is "+weather); /lambda expression has only one statement, you can omit the curly braces, and when you need to return a value, you can also omit the return keyword Ld.add ((A, b)->a+b);}}
The type of the lambda expression, also known as the target type, must be a functional interface to the target type of the lambda expression. A functional interface (functional Interface) represents an interface that contains only an abstract method, and a functional interface can have multiple class methods and default methods. The @functionalinterface annotation in the comment (Annotation) that java8 begins with is used to prompt the compiler to perform a function-interface check.
Java pre-defined a number of functional interfaces, such as runnable, Actionlistenner are functional interfaces, there are also a large number of functional interfaces under the Java.util.function package. (Package Java.util.function
Functional interfaces provide target types for lambda expressions and method references)
For example, the package below typically contains the following four classes of interfaces:
<1>xxxfunction: This type of interface is typically used for conversion processing of specified data. Typically contains an apply abstract method that handles the conversion of a parameter and then returns a new value, such as the Applyasint method for Doubletointfunction (Doubletointfunctiondemo.java demo).
<2>xxxconsumer: This interface usually contains an apply abstract method, similar to the Xxxfunction interface, except that there is no return value.
<3>xxxpredicate: This type of interface usually contains a test () abstract method, which is often used to determine the parameters of the filter data, and then return a Boolean value,
<4>xxxsupplier: This type of interface usually contains a getasxxx () abstract method that does not require parameters, and the method returns a data according to a logical algorithm.
For example, the use of the Doubletointfunction interface under the Java.util.function package is as follows:
Import java.util.function.*;p ublic class doubletointfunctiondemo{public void Doubletoint (double D, Doubletointfunction dti) {int tmp=dti.applyasint (d); System.out.println (d+ "----->" +tmp);} public static void Main (string[] args) {Doubletointfunctiondemo a=new doubletointfunctiondemo (); A.doubletoint (1.20, (d )->{return (int) d;});}}
Here is just a few simple examples of the Java lambda expression, the future will continue to update, I hope to engage in the development of friends can communicate, and constantly improve!
New features for lambda expression--java8 case Study (1)