<分区>
我对 Lambda 不熟悉,教科书(简述 java)是这样说的:
Only certain types are eligible to be the target of a lambda . Target types are also called functional interfaces and they must:
- Be interfaces
- Have only one nondefault method(but may have other methods that are default)
这本书给了我一个例子:
File dir =new File("/src");
String[] fileList =dir.list((d,fName)->fName.endWith(".java"))
我知道这个 lambda 会转换成一个 FilenameFilter
接口(interface),它是一个 @FunctionalInterface
它确实符合书中的描述
不过,当 sort()
期望收到一个 Comparator
int n =12;
Function<String,String[]> f = (s) -> {
String[] ans = new String[n];();
for (int i = 0; i < n; i++) {
ans[i] = s;
}
return ans;};
Arrays.sort(f.apply("colin"),(String s1 ,String s2)->{
return s1.length() - s2.length();});
我查看了它的源代码,发现 Comparator
是一个 @FunctionInterface
但是它有两个非默认的方法compare
和equals
。和书上说的不一样。