Lambda expressions are used to simplify the writing of anonymous inner classes
Note: An interface can only have one abstract method, because if there are multiple abstract methods, the system cannot tell which method our lambda expression rewrites.
Lambda expressions can also continue to be omitted
String[] arr = {"a", "bsdf", "qae", "asqweq"};
int[] arrint = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
arrint[i] = arr[i].length();
}
int[] arrint2 = Arrays.copyOf(arrint, arr.length);
Arrays.sort(arrint2);
for (int i = 0; i < arrint2.length; i++) {
for (int j = i; j < arrint.length; j++) {
if (arrint2[i] == arrint[j]){
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
break;
}
}
}
System.out.println(Arrays.toString(arr));
// Lambda完整表达式
Arrays.sort(arr, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
});
// Lambda简易表达式
Arrays.sort(arr, (o1, o2) -> o1.length() - o2.length());