Study number 2016-2017-2 "Java program design" x-Week Learning Summary
# # Textbook Learning Content Summary
Overview of LAMBDA syntax
String[] names={"Justin", "Caterpillar", "Bush"}
Array.Sort (names,new comparator<string> () {
public int Compare (String name1,string name2) {
Return name1.length ()-name2.length;
});
Arrays's sort visit can be used to sort, except that you have to know the order of comparison of two elements, and the sort () rules to manipulate java.Util.Compatator to illustrate this matter.
Through the variable bylength, it is true that the sorting intent is clear many, but the operation of the anonymous class is still lengthy, for example, the declaration bylength has been written once compatator<string>, anonymous class operation is also written once compatator< String> can be written as
Compatator<string>bylength= (String name1,string name2)-.>name1.length ()-name2.length ();
Duplicate compatator<string> information is removed from the right side of the equal sign, the original anonymous class method has only one to operate, when using the Lamaba expression, it is actually compatator<string> declaration from the left of the equals sign to know, The Lamba expression actually operates on the Compare () method in Compatator<string>, as well as the duplicate information, since the declaration variable uses the COMPATATOR<STRING> Why do I have to declare a string again on the Lamba expression argument? In fact, it is not necessary, because the compiler can infer the type of name1 and name2 from the declaration type of the bylength variable, which can be
Compatator<string> bylength= (name1,name2)->name1.length-name2.length ();
You can put it directly into the sort method of array.
The compiler can infer from names that the second parameter type of the sort () method is actually compatator<string>, so name1 and name2 do not have to declare the type, and the program code is much more concise than the initial anonymous class notation.
If many places have a need to sort by string length, this can be used. Encountered in the same method, like the previously used byname local variables, if the class in the same method to be shared, with a byname data member, because the byname to reference the data variable has no state problem, so declared as static more appropriate, if more than one class to implement sharing, Set to public static for example
Package CC. OpenHome
Import. Java. Util.arrays;
public class strinforderdemo{
public static void Main (string[] args) {
String[] names={"Justin", "Caterpillar", "Bush"};
Arrays.sort (names,stringorder::bylength);
System.out.peintIn (arrays.tostring (names));
}
}
In addition to the method name, the Bylength method is the same as the Compare method of Compatator, you only have the Lamba expression to pass the parameter S1 and S2 to the Bylength method, can you directly reuse the Bylength method operation is not better?
One of the main considerations for maintaining compatibility with existing APIs while introducing Lamba in Java. Method Reference Properties
While introducing Lamba in Java, maintaining compatibility with existing APIs is a major consideration. By reusing existing method operations, you can avoid writing Lamba expressions everywhere. The above example uses a form of method reference, which refers to the existing static method.
Now look at another requirement, if you sort the names by dictionary order? Because Stringorder has been defined, so write
String[] names={"Justin", "Caterpillar", "Bush"};
Arrays.sort (names,stringorder::bylexicography);
In Stringorder's Bylexicography () method operation, only the Comparto method that will invoke string is the parameter S1 as the CompareTo's recipient, and the second parameter S2 as the Compare () argument, in which case In fact, we can refer directly to the CompareTo method of the string class, for example
Import Java.util.Arrays;
public class stringdemo{
public static void Main (string[] args) {
String[] names={"Justin", "Caterpillar", "Bush"};
Arrays.sort (Names,string::compareto);
System.out.printIn (array.tostring (names));
}
}
Similarly, you want to sort the names in a dictionary order, but ignore the case differences, or pass the Stringorder static method, just refer to the Comparetoignorecase method in string:
String[] names={"Justin", "Caterpillar", "Bush"};
Array.Sort (names,string::comparetoignorecase);
It can be easily observed that the method reference not only avoids repeating the LAMDBA expression, but also makes the program code clearer. Here is just the first taste of the following lamdba sweetness, about LAMDBA There are more details, follow-up will pay attention to explore.
LAMDBA-expression
The following program code is known,
Comparator<string> bylength =
(String name1,string name2)->name1.length ()-name2.length ();
Can be disassembled into two parts, the right side of the equals sign is a LAMDBA expression, the left side of the equals sign is the target type of the LAMDBA expression, and the LAMDBA expression to the right of the equals sign:
(String name1,string name2)->name1.length ()-name2.length ();
This LAMDBA expression accepts two parameters name1 and name2, the arguments are of type string, and the current right defines a single expression that returns results, and if the operation is complex, you must use a multiline description to add the {} definition description block. If there is a return value, you must add a return, for example:
(String name1,string name2)->{
String Name1=name1.trim ();
String Name2=name2.trim ();
...
Return name1. Length ()-name2. Length ();
}
In a lambda expression, you must write down parentheses even if you do not accept any arguments. For example
(), "Justin"//Do not accept any parameters, return string
()->system.out.printin ()//Do not accept parameters, no return value
In the case of only lamdba expressions, the parameter type must be written out, and if there is a target type, the compiler can infer the type, without having to write out the argument type of the LANBDA expression, which is actually a string, so you do not have to write the argument type.
Compatator<string>bylength= (name1,name2)->name1.length ()-name2.length ();
The LAMDBA expression itself is neutral, does not represent any type of instance, and the same lambda expression can be used to represent the operands of different target types, for example, the above example represents the compparator<string> operation.
Summarize what you learned this week as simple as possible.
Try not to transcription, waste time
Read it, do not understand, learn to remember the experience
# # Problems and solutions in textbook learning
-Problem 1:xxxxxx
-Problem 1 Solution: XXXXXX
-Problem 2:xxxxxx
-Problem 2 Solution: XXXXXX
- ...
# # Problems in code debugging and resolution process
-Problem 1:xxxxxx
-Problem 1 Solution: XXXXXX
-Problem 2:xxxxxx
-Problem 2 Solution: XXXXXX
- ...
# # [Code Hosting] (Code Cloud Learning Project LINK)
Http://git.oschina.net/tianmaxingkomg/xinjianxianmu
(run result of statistics.sh script)
# # last week quiz summary of the wrong title
-Wrong question 1 and why, understand the situation
-Wrong question 2 and why, understand the situation
- ...
# # Pairs and mutual comments
# # # Scoring Standard
1. Correct use of markdown syntax (plus 1 points):
-Do not use markdown without extra points
-No extra points for grammatical errors (links cannot be opened, table is wrong, list is incorrect ...) )
-No extra points for typesetting confusion
2. Templates
20155335 Eucun The seventh week of Java programming