用java8重写Arrays.sort(oldWay, new Comparator<String>(){@Override public int compare(String s1, String s2)});

Reference https://www.liaoxuefeng.com/article/001411306573093ce6ebcdd67624db98acedb2a905c8ea4000/

Java 8 finally introduced lambda expressions, marking another small step toward functional programming in Java.

In the code before Java 8, in order to implement an interface with a method, it is often necessary to define an anonymous class and override the interface method, and the code is very bloated. For example, the common Comparatorinterface:

String[] oldWay = "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 an interface with only one method, in Java 8, it can now be treated as a function, simplified in lambda notation 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 the new keyword lambda, Java 8 uses ()->{}this strange notation to denote lambda functions. The function type does not need to be declared and can be automatically deduced from the method signature of the interface. For the above lambda function:

(s1, s2) -> {
    return s1.toLowerCase().compareTo(s2.toLowerCase());
});

Parameters are Comparator<String>automatically deduced Stringtypes, and the return value must also conform to the interface's method signature.

In fact, the lambda expression ends up being compiled into an implementation class, but the syntax is simplified.

change it up a bit

  Reference: https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993?tpId=13&tqId=11185&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
String[] strArr={"32","43"}; 
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); }
Rewrite it into the following form in java8: 
String[] strArr={"21","33"}; Arrays.sort(strArr,(a,b)->{ String c=a+""+b; String d=b+""+a; return c.compareTo(d); });
上面代码实现的是:
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]+""+numbers[i]);
if(a>b){
int temp=numbers[i];
numbers[i]=numbers[j];
numbers[j]=temp;
}
}
}

 

For the large number of single-method interfaces in the standard library that comes with Java, many have been marked as @FunctionalInterfaceindicating that the interface can be used as a function.

Taking the Runnableinterface as an example, in many cases, the code that does the work has not yet defined the code of the class, and now it can 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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325133244&siteId=291194637