Reference Https://www.liaoxuefeng.com/article/001411306573093ce6ebcdd67624db98acedb2a905c8ea4000/
Java 8 has finally introduced a lambda expression, which marks a small step in Java's way toward functional programming.
In previous Java 8 Code, in order to implement an interface with a method, it is often necessary to define an anonymous class and to replicate the interface method, the code looks bloated. For example, common Comparator
interfaces:
"Improving code with Lambda expressions in Java 8".split(" ");Arrays.sort(oldWay, new Comparator<String>() { @Override public int compare(String s1, String s2) { // 忽略大小写排序: return s1.toLowerCase().compareTo(s2.toLowerCase()); }});System.out.println(String.join(", ", oldWay));
For interfaces with only one method, in Java 8, it is now possible to think of it as a function, with the lambda representation simplifying as follows:
String[] newWay = "Improving code with Lambda expressions in Java 8".split(" ");Arrays.sort(newWay, (s1, s2) -> { return s1.toLowerCase().compareTo(s2.toLowerCase());});System.out.println(String.join(", ", newWay));
Instead of introducing a new keyword lambda, Java 8 uses ()->{}
this strange notation to represent a lambda function. The function type does not need to be declared, it can be deduced automatically by the method signature of the interface, for the above lambda function:
(s1, s2) -> { return s1.toLowerCase().compareTo(s2.toLowerCase());});
Parameters Comparator<String>
are automatically derived from the String
type, and the return value must conform to the method signature of the interface.
In fact, the lambda expression is eventually compiled into an implementation class, but the syntax is simplified.
A little change.
Reference:https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993?tpid=13&tqid=11185&rp=2& Ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
< Span style= "COLOR: #000000" > < Span style= "COLOR: #0000ff" >string["strarr={" + "," $ "};
Arrays.sort (strarr,new comparator<string> () {@Override public int compare (string s1, string s2) {string C1 = S1 + S2; String c2 = s2 + S1; return C1.compareto (C2);}
string[] strarr={"21", "33"}; Arrays.sort (Strarr, (A, b)->{String c=a+B; String d=b+A; return C.compareto (d);}); < Span style= "COLOR: #000000" >
The code above implements:
for (int I=0;i<numbers. length;i++) {
for (int j= I+1;j<numbers. length;j++) {
Long a=long. ValueOf (Numbers[i]+ "+numbers[j]);
Long B=long. valueof (Numbers[j]+ if (a>b) {
int temp=numbers[i];
Numbers[i]=numbers[j];
Numbers[j]=temp;
}
}
}
Many of the single-method interfaces in the Java-brought standard library have been marked to @FunctionalInterface
indicate that the interface can be used as a function.
In the Runnable
case of interfaces, many times the code of the work does not yet define the class code, which can now be implemented with Lambda:
public static void main(String[] args) { // old way: Runnable oldRunnable = new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + ": Old Runnable"); } }; Runnable newRunnable = () -> { System.out.println(Thread.currentThread().getName() + ": New Lambda Runnable"); }; new Thread(oldRunnable).start(); new Thread(newRunnable).start();}
In future Java code, more and more expressions will appear ()->{}
.
Rewrite Arrays.sort with Java8 (Oldway, New comparator<string> () {@Override public int compare (string s1, string s2)});