英文:
How come Arrays.sort(stringArray, String::compareToIgnoreCase) is using compareToIgnoreCase() instead of Comparator
问题
在Arrays.sort(stringArray, String::compareToIgnoreCase)中,使用了compareTo()方法,其返回类型是String,而不是需要两个参数的Comparator。
public static void main(String[] args) {
String[] stringArray = { "Steve", "Rick", "Aditya", "Negan", "Lucy", "Sansa", "Jon" };
Arrays.sort(stringArray, String::compareToIgnoreCase);
for (String str : stringArray) {
System.out.println(str);
}
}
英文:
Need some clarification on how Arrays.sort(stringArray, String::compareToIgnoreCase) is using<br/> compareTo() whose return type is String and not Comparator which takes two arguements.
public static void main(String[] args) {
String[] stringArray = { "Steve", "Rick", "Aditya", "Negan", "Lucy", "Sansa", "Jon" };
Arrays.sort(stringArray, String::compareToIgnoreCase);
for (String str : stringArray) {
System.out.println(str);
}
}
答案1
得分: 0
一个Comparator<String>
是一个函数式接口,接受两个String
参数并返回一个int
。在需要这样的比较器时,可以使用具有相同特征的lambda表达式代替。
compareToIgnoreCase
方法具有相同的特征,其中第一个参数是调用该方法的对象,第二个参数是方法的参数。该方法返回一个int
(而不是String
)。因此,它具有Comparator<String>
的形式。
由于所有这些以及函数式接口的工作方式,人们可以通过利用compareToIgnoreCase
在lambda表达式中提供一个Comparator<String>
对象,将其提供给Arrays.sort
,就像这样:
Arrays.sort(stringArray, (s1, s2) -> s1.compareToIgnoreCase(s2));
Java编译器提供了一种构建这种形式的lambda的简写方式,称为方法引用。当通过String::compareTo
这种形式传递到compareTo
方法的引用时,编译器将构建相同的lambda(因此是一个Comparator<String>
对象)。编译器会意识到它可以在lambda/比较器的第一个参数(称为“接收者”)上调用提供的方法,并将lambda/比较器的第二个参数作为方法接受的单个参数传递。该方法将返回一个int
,这就是任何Comparator
应该返回的内容。因此,您可以这样做:
Arrays.sort(stringArray, String::compareToIgnoreCase);
神奇!
英文:
A Comparator<String>
is a functional interface that takes two String
parameters and returns an int
. Where such a Comparator is required, a lambda with the same characteristics can be used instead.
The compareToIgnoreCase
method has the same characteristics, where the first parameter is the object that the method is called on, and the second parameter is the parameter to the method. The method returns an int
(not a String
). So it has the form of a Comparator<String>
.
Because of all this, and the way functional interfaces work, one can supply a Comparator<String>
object to Arrays.sort
via a lambda expression that utilizes compareToIgnoreCase
, like this:
Arrays.sort(stringArray, (s1, s2) -> s1.compareToIgnoreCase(s2));
The Java compiler offers a shorthand way of constructing lambdas of this form with what's called a Method Reference. The compiler will construct this same lambda (and so a Comparator<String>
object) when a reference to the compareTo
method is passed in via the form String::compareTo
. The compiler will realize that it can call the provided method on the lambda/Comparator's first parameter (called the "receiver"), and pass the lambda/Comparator's second parameter as the single parameter that the method takes. The method will return an int
, which is what any Comparator
should return. And so you can do:
Arrays.sort(stringArray, String::compareToIgnoreCase);
Magic!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论