Some of the company's new projects have adopted Java 8, which has the power to study the Java 8 the legendary Big kill device. The first improvement that is mentioned in the Java 8 documentation is lambda, so start with it. Many of the great gods of the community have been studying for years, bye. Where there is inaccuracy, look.
Why Lambda?
Write the Java code a little bit more succinctly, and more plainly, the code is shorter. So simple? Core, yes, but there may be some extra benefits. In theory, lambda is not a necessity, and the code written is more elegant and concise. One of the most popular problems with Java is the large amount of code (a lot of it is useless, just the syntax requires it), and lambda can thin the Java code. The Java Community's voice to Lambda has always been high, and Java 8来, and the project has been used in succession. Sprinkle flowers ...
What exactly is a lambda?
in Java, Lambda is shorthand for the function interface (functional Interface), which is essentially a function interface .
- what is a function interface?
A function interface is a special interface that is an interface with only one abstract method
- What is an abstract method?
Abstract methods are methods that are not implemented, that is, the method parameters are not followed by curly braces
So, Lambda is an interface, and a class, not a method inside a class. Quite surprised, remember the python language inside Lambda's nickname is anonymous function, is a function, not a class AH. Guess how Java is designed to weigh the results of forward-compatible factors.
Therefore, any lambda in Java must have a corresponding function interface, the function interface is the type of lambda, and Lambda is an instance of the function interface. The function interface needs to be declared well in advance, and lambda is written when it is used. How annoying, using lambda to write the function interface first? Yes, but with this in mind for JDK design, there are a number of commonly used function interfaces available in advance, all under the Java.util.function package, such as Predicate,consumer.
Lambda syntax
Lambda consists of three parts, parameters,->, method body
Parameters
(ParamType1 p1, ParamType2 p2, ...)
Parameter types can be saved, changed to (P1, p2, ...)
The parentheses can also be saved with only one parameter, p1
method Body
An expression or a block of statements, the return statement does not have to be written
The expression will have a result of the calculation
The statement block is enclosed in curly braces (multiple statements can be written), with only one statement and the return value of the abstract method of the function interface is void, the braces can be saved
Lambda example
public class Lambda {public static void main (string[] args) {list<material> materials = new ARRAYLIST< ; Material> (); Materials.add (New Material ()); Materials.add (New Material ()); Materials.add (New Material ()); Materials.add (New Material ()); Materials.add (New Material ()); Dosomthing (Materials, (Material m), System.out.println ("Print" + M.getid ())); } public static void Dosomthing (list<material> materials, Functionalinterfacedemo Demo) {for ( Material material:materials) {demo.print (Material); }} static class Material {public static int idstore = 1; private int id; Public Material () {id = idstore++; } public int getId () {return id; } public void setId (int id) {this.id = ID; }} static interface Functionalinterfacedemo {void print (Material matErial); }}
Functionalinterfacedemo is the function interface.
Conclusion
The parameter type can not be written and is not used at first. However, the JDK will infer the function interface of the lambda at compile time , thus knowing the abstract method information. Also note that Lambda does not generate a new scope (although there are curly braces), which is the same scope as the lambda. Lambda is not a necessity, but it is definitely not a luxury, other languages have been used for many years, up to the Java 8 students use up.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Lambda of Java 8