It's cool to see the HelloWorld tutorial of a LAMBDA by chance when you brush Weibo. Share. Effects Sample
The overall look is simple code, as follows
Btn.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (View v) {
System.out.println ("Hello Lambda");
...
}
});
Lambda expression
Btn.setsetonclicklistener (View v)-> {
System.out.println ("Hello Lambda");
...
}
);
The previous writing
new Thread (new Runnable () {
@Override public
void Run () {
System.out.println ("Hello Lambda ");
}
). Start ();
Lambda expression writing (only one line can remove the code block braces)
new Thread (()-> System.out.println ("Hello Lambda")). Start ();
lambda expression Template
(T args)-> {
//code block
}
A lambda expression consists of three parts:
-Parameter: (T args) is the parameter part of this lambda expression, including parameter type and parameter name, nullable, parentheses indispensable
-Arrow:, indispensable
-code block: is the code contained in "{}". When there is only one line of code in the code block, the curly braces can be omitted and the semicolon ellipsis principle in the code block
A lambda expression is not a new syntax, but a shorthand literal, in which a programmer can omit a "certain" work in an anonymous inner class, and then a simplified expression is restored by the JVM by inference. step-by-Step explanation
In contrast to the age of two students public
void sort (student[] students) {
Arrays.sort (students, new comparator<student> () {
@Override public
int compare (Student stu1, Student stu2) {return
integer.compare (Stu1.age, stu2.age);
}
});
}
As the example above, the second parameter "must" be the new Comparator<t> () {} type, you can remove the first "certain"
Compare age of two students public
void sort (student[] students) {
Arrays.sort (students,
@Override public
int Compare (Student stu1, Student stu2) {return
integer.compare (Stu1.age, stu2.age);
}
);
New Comparator<t> () {} "must" to override the Compare method, and "must" return the int type, then the method name can also be removed
Compare age of two students public
void sort (student[] students) {
Arrays.sort (students, Student stu1
, Student stu2) { Return
Integer.compare (Stu1.age, stu2.age);
}
);
This time still will be the error, we in the argument list and code block between the arrow (->) to form a complete lambda expression can be compiled.
In contrast to the age of two students public
void sort (student[] students) {
Arrays.sort (students, Student stu1
, Student stu2)- > {return
integer.compare (Stu1.age, stu2.age);
}
);
If there is only one line of code in the code block, you can remove the curly braces and return it (since the Compare method is definitely returning int), and the last semicolon in the line is removed. And the extra blank lines can be merged into one row.
Compare age of two students public
void sort (student[] students) {
Arrays.sort (students, Student STU1, Student stu2)-> Integer.compare (Stu1.age, Stu2.age));
The parameter type "bound" in the parameter list of the Compare method is the type of the first parameter in the Arrays.sort () method, so it can be omitted because the JVM can help us derive it.
In contrast to the age of two students public
void sort (student[] students) {
Arrays.sort (students, STU1, STU2)-> Integer.compare ( Stu1.age, Stu2.age));
Simplified completion. Now let's compare the simplified and simplified code before you feel
In contrast to the age of two students public
void sort (student[] students) {
//simplified pre-
arrays.sort (students, new comparator<student > () {
@Override public
int compare (Student stu1, Student stu2) {return
integer.compare (Stu1.age, stu2.age);
}
);
Simplified post-
arrays.sort (students, (STU1, STU2)-> integer.compare (Stu1.age, Stu2.age));
Other effects
Lambda expressions can also be used to assign values.
Runnable r = ()-> System.out.println ("Hello Lambda");
New Thread (R). Start ();
Lambda Use qualification
Simply put, downloading the latest eclipse will support lambda syntax.
I'm an android party, Java 8 supports lambda expressions, and Android only supports Java 7, and if you want to use Android, use the Gradle-retrolambda plugin.
Finally, we recommend that you go to the video: The HelloWorld video tutorial on Lambda
Reference:
The HelloWorld video tutorial of the LAMBDA
"Java 8 Lambda and its application in Android development"
"How to use lambda expressions in Android Programming"