如何将以下比较器翻译为 `Comparator.comparing`?

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

How to translate following comparator into Comparator.comparing?

问题

以下是您要求的翻译内容:

我的问题可能不太清楚,所以让我通过示例来澄清:

Arrays.sort(arr, new Comparator<String>(){
    public int compare(String a, String b){
        return (b + a).compareTo(a + b);
    }
});

我想使用Comparator.comparing。我尝试了以下代码:

Arrays.sort(arr, Comparator.comparing((a, b) -> (b + a).compareTo((String)a + b)));

但是我遇到了错误 - lambda 表达式中的返回类型不正确。如何修复这个问题?

英文:

My question may be unclear so let me clear it out by example:

Arrays.sort(arr, new Comparator&lt;String&gt;(){
    public int compare(String a, String b){
        return (b + a).compareTo(a + b);
    }
});

I want to use the Comparator.comparing. I tried out the following:

Arrays.sort(arr, Comparator.comparing((a, b) -&gt; (b + a).compareTo((String)a + b)));

I get an error - bad return type in lamdba expression. How to fix this ?

答案1

得分: 6

Comparator.comparing方法期望一个类型为FunctionkeyExtractor。您只需要一个lambda表达式来实现在这里的Comparator<String>接口:

Arrays.sort(arr, (a, b) -> (b + a).compareTo(a + b));
英文:

Comparator.comparing method expects a keyExtractor of type Function. You just need a lambda to implement the Comparator&lt;String&gt; interface here:

Arrays.sort(arr, (a, b) -&gt; (b + a).compareTo(a + b));

答案2

得分: 4

这是不可能转换为一个比较调用的,因为它不是一个有效的比较器:它不满足比较器的约定,而且你不应该把它用作比较器。

为了证明这一点,该比较器将会将每个字符串与“”视为相等,但并不是每个字符串都相等。这违反了传递性原则。

英文:

It is impossible to turn that into a call to comparing because it's not a valid Comparator: it doesn't satisfy the Comparator contract, and you should never use it as a Comparator.

To prove it, that Comparator will compare every string as equal to "", but not every string will be equal to each other. That violates the transitivity property.

答案3

得分: 1

已经回答过了,您的匿名类实现可以简化为lambda表达式:

Arrays.sort(arr, (a, b) -> (b + a).compareTo(a + b));

如果您坚持使用Comparator.comparing(),请记住它具有特定的参数,这些参数不适用于您的排序问题。

Arrays.sort(arr, Comparator.comparing(
    Function.identity(),                  // keyExtractor, 比较什么
    (a, b) -> (b + a).compareTo(a + b))); // keyComparator, 如何比较

这是一种使用Comparator.comparing的解决方案,它使用keyExtractorFunction.identity(),返回输入本身(与str -> str lambda表达式相同),因为您仍然希望以不同的方式比较字符串,但是要使用自定义的Comparator来指定。因此,满足您需求的唯一正确排序数组的方法是省略keyExtractor的简化版本:

Arrays.sort(arr, (a, b) -> (b + a).compareTo(a + b));

...这最终就是我们开始的地方。

英文:

As already answered, your anonymous class implementation can be shortened into a lambda expression:

Arrays.sort(arr, (a, b) -&gt; (b + a).compareTo(a + b));

If you insist on using Comparator.comparing(), remember it has specific parameters that don't fit your sorting problem.

  • The Comparator.comparing(keyExtractor) returns a specified Comparator for certain key based on the natural way of comparison (Comparator.naturalOrder). Your method doesn't say what is compared, but how it is.

  • The Comparator.comparing(keyExtractor, keyComparator) looks a bit better because you can specify how the specified keys are compared using keyComparator. You can use your logics of comparing and you conclude to:

     Arrays.sort(arr, Comparator.comparing(
         Function.identity(),                  // keyExtractor, WHAT is compared
         (a, b) -&gt; (b + a).compareTo(a + b))); // keyComparator, HOW is it compared
    

    This is a solution using Comparator.comparing that uses a keyExtractor the Function.identity() returning the input back (the same like str -&gt; str lambda expression) since you want still compare the Strings but in a different way specified with a custom Comparator, therefore the only correct way to sort the array as you need is the simplified version omitting the keyExtractor:

     Arrays.sort(arr, (a, b) -&gt; (b + a).compareTo(a + b));
    

    ... which is finally where we started at.

huangapple
  • 本文由 发表于 2020年4月5日 22:07:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/61043870.html
匿名

发表评论

匿名网友

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

确定