How come Arrays.sort(stringArray, String::compareToIgnoreCase) is using compareToIgnoreCase() instead of Comparator

huangapple go评论81阅读模式
英文:

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 = { &quot;Steve&quot;, &quot;Rick&quot;, &quot;Aditya&quot;, &quot;Negan&quot;, &quot;Lucy&quot;, &quot;Sansa&quot;, &quot;Jon&quot; };

    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&lt;String&gt; 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&lt;String&gt;.

Because of all this, and the way functional interfaces work, one can supply a Comparator&lt;String&gt; object to Arrays.sort via a lambda expression that utilizes compareToIgnoreCase, like this:

Arrays.sort(stringArray, (s1, s2) -&gt; 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&lt;String&gt; 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!

huangapple
  • 本文由 发表于 2020年9月21日 08:48:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63984937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定