Java 中开箱即用的直接比较器
我有一个方法,其参数之一需要一个 Comparator
。我想传递一个进行正常比较的Comparator
和一个进行相反操作的反向比较器。
java.util.Collections
提供了一个reverseOrder()
,这对于反向比较很有用,但我找不到任何正常的Comparator
。
我想到的唯一解决方案是Collections.reverseOrder(Collections.reverseOrder())。但我不喜欢它,因为里面调用了 double 方法。
当然,我可以像这样编写一个 NormalComparator
:
public class NormalComparator<T extends Comparable> implements Comparator<T> {
public int compare(T o1, T o2) {
return o1.compareTo(o2);
}
}
但我真的很惊讶 Java 没有现成的解决方案。
I have a method which needs a Comparator
for one of its parameters. I would like to pass a Comparator
which does a normal comparison and a reverse comparator which does in reverse.
java.util.Collections
provides a reverseOrder()
this is good for the reverse comparison, but I could not find any normal Comparator
.
The only solution what came into my mind is Collections.reverseOrder(Collections.reverseOrder())
. but I don't like it because the double method calling inside.
Of course I could write a NormalComparator
like this:
public class NormalComparator<T extends Comparable> implements Comparator<T> {
public int compare(T o1, T o2) {
return o1.compareTo(o2);
}
}
But I'm really surprised that Java doesn't have a solution for this out of the box.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
大多数可以指定
Comparator
的地方也有一个根本不使用Comparator
的版本,在这种情况下它使用自然顺序(即它期望所有对象实现>可比较
并使用compareTo
)。因此,通常的解决方案是根本不指定
Comparator
。您是否有仅支持Comparator
方法的特定情况?如果您绝对需要,Google 收藏集(以及 Guava,它是 Google Collections 的超集)提供
Ordering.natural()
返回Ordering 对象
,表示由
Comparable
接口定义的自然顺序。Ordering
实现了Comparator
,因此您可以简单地使用它。Most places where you can specify a
Comparator
also have a version without using aComparator
at all in which case it uses the natural order (i.e. it expects all objects to implementComparable
and usescompareTo
).So the usualy solution to this is to not specify a
Comparator
at all. Do you have a specific case where only theComparator
approach is supported?If you absolutely need it, the Google Collections (as well as Guava, which is a superset of the Google Collections) provides
Ordering.natural()
which returns aOrdering
object that represent the natural order as defined by theComparable
interface.Ordering
implementsComparator
, so you can simply use that.我想它在某些情况下会很有用......就像你的情况一样。但在大多数用例中,应用程序只需直接使用对象的
compareTo
方法即可。通过Comparator
对象间接进行是没有任何作用的……大多数时候。我的猜测是,这些 Java API 的设计者并没有认为您的用例重要到足以直接支持。此外,您的实现只有四行代码。
Java 类库并不完美。学会忍受它:-)。
I suppose it would be useful in a few cases ... like yours. But in most use-cases an application would simply use the object's
compareTo
method directly. Indirecting via anComparator
object would serve no purpose ... most of the time.My guess is that the designers of those Java APIs did not consider your use-case important enough to support directly. Besides, your implementation is only four lines of code.
The Java class libraries are not perfect. Learn to live with it :-).
对于反向排序,请使用
Collections.reverseOrder()
...For reverse ordering use
Collections.reverseOrder()
...通常不需要自然顺序
Comparator
,因为通常有一个需要Comparable的重载。
。您始终可以按照Collections.reverseOrder()
设置的示例编写如下内容:然后您可以编写如下内容:
There is usually no need for a natural order
Comparator<T>
, since usually there's an overload that takes aComparable<T>
. You can always follow the example set byCollections.reverseOrder()
and write something like this:You can then write something like: