import java.util.Arrays;
import java.util.*;
public class Lambda
{
public static void main(String[] args)
{
String[] arr1 = new String[]{"java" , "fkava" , "fkit" , "ios" , "android"};
Arrays.parallelSort(arr1 , (o1 , o2) -> o1.length() - o2.length());
System.out.println(Arrays.toString(arr1));
}
}
我没弄明白Arrays.parallelSort(arr1 , **)第二个参数的意思,尝试把Lambda表达式转换成匿名内部类,可一直编译出错。
下面是我改的代码
import java.util.Arrays;
import java.util.*;
public class Lambda
{
public static void main(String[] args)
{
String[] arr1 = new String[]{"java" , "fkava" , "fkit" , "ios" , "android"};
Arrays.parallelSort(arr1, new Comparator()
{
public int compare(String o1, String o2)
{
return o1.length() - o2.length();
}
});
System.out.println(Arrays.toString(arr1));
}
}
编译出错应该是少个泛型,要不Comparator才推导不出o1,o2的类型。