【Java的Lambda表达式】请问这个用Lambda表达式写的代码如何改成用匿名内部类的代码呢?
迷茫
迷茫 2017-04-17 15:04:34
0
1
399

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));
}

}

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
阿神

The compilation error should be due to the lack of generics, otherwise the Comparator cannot deduce the types of o1 and o2.

new Comparator<String>() {}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!