英文:
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<String>(){
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) -> (b + a).compareTo((String)a + b)));
I get an error - bad return type in lamdba expression. How to fix this ?
答案1
得分: 6
Comparator.comparing
方法期望一个类型为Function
的keyExtractor
。您只需要一个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<String>
interface here:
Arrays.sort(arr, (a, b) -> (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()
,请记住它具有特定的参数,这些参数不适用于您的排序问题。
Comparator.comparing(keyExtractor)
根据自然比较方式(Comparator.naturalOrder
)返回指定的用于某个键的Comparator
。您的方法没有说明在比较什么,而是说明了如何比较。Comparator.comparing(keyExtractor, keyComparator)
看起来更好一些,因为您可以使用keyComparator
指定如何比较指定的键。您可以使用自己的比较逻辑,得出如下结论:
Arrays.sort(arr, Comparator.comparing(
Function.identity(), // keyExtractor, 比较什么
(a, b) -> (b + a).compareTo(a + b))); // keyComparator, 如何比较
这是一种使用Comparator.comparing
的解决方案,它使用keyExtractor
为Function.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) -> (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 specifiedComparator
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 usingkeyComparator
. You can use your logics of comparing and you conclude to:Arrays.sort(arr, Comparator.comparing( Function.identity(), // keyExtractor, WHAT is compared (a, b) -> (b + a).compareTo(a + b))); // keyComparator, HOW is it compared
This is a solution using
Comparator.comparing
that uses akeyExtractor
theFunction.identity()
returning the input back (the same likestr -> str
lambda expression) since you want still compare the Strings but in a different way specified with a customComparator
, therefore the only correct way to sort the array as you need is the simplified version omitting thekeyExtractor
:Arrays.sort(arr, (a, b) -> (b + a).compareTo(a + b));
... which is finally where we started at.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论