Java 标准api中是否存在自然比较器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3241063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does a natural comparator exist in the standard api?
提问by Yishai
I need a comparator as part of a strategy pattern that can either use the natural ordering of the objects or some custom ordering. For the natural ordering case, I wrote a simple comparator:
我需要一个比较器作为策略模式的一部分,它可以使用对象的自然排序或一些自定义排序。对于自然排序的情况,我写了一个简单的比较器:
private static class NaturalComparator<T extends Comparable<? super T>> implements Comparator<T> {
@Override
public int compare(T o1, T o2) {
return o1.compareTo(o2);
}
}
Seems simple enough, but I was wondering if anyone knew of one in the standard API. I looked at TreeMap, and it does it without such a class, so when that code was written, the apparent answer would be no, but perhaps it was added later.
看起来很简单,但我想知道是否有人知道标准 API 中的一个。我查看了 TreeMap,它没有这样的类,所以当编写该代码时,明显的答案是否定的,但也许它是稍后添加的。
采纳答案by Julien
Added to Comparator in Java 8:
static <T extends Comparable<? super T>> Comparator<T> naturalOrder()
Use it like this, for example:
像这样使用它,例如:
Comparator<Double> natural = Comparator.<Double>naturalOrder();
return natural.compare(1.0, 1.1));
回答by Uri
I am not familiar with a default comparator in Java, but obviously, Comparator to compareTo is often a mere wrapper.
我不熟悉 Java 中的默认比较器,但显然,Comparator 到 compareTo 通常只是一个包装器。
There is no general of "natural ordering" in the standard API, although certain built in types, like numbers, have an implementation of compareTo which then becomes their natural ordering.
在标准 API 中没有“自然排序”的一般性,尽管某些内置类型(如数字)具有 compareTo 的实现,然后成为它们的自然排序。
TreeMap
and TreeSet
and all these should throw a RuntimeException if the object you put in does not implement Comparable. Thus, for example, you could throw in strings or numbers but not another collection.
TreeMap
TreeSet
如果您放入的对象没有实现 Comparable ,并且所有这些都应该抛出一个 RuntimeException。因此,例如,您可以输入字符串或数字,但不能输入另一个集合。
The code of TreeMap
does not use a comparator if one is not available - it uses compareTo
instead. To use compareTo
, it does a cast to Comparable
, which is the source of the exceptions.
TreeMap
如果比较器不可用,则代码不使用比较器——而是使用compareTo
。要使用compareTo
,它会强制转换为Comparable
,这是异常的来源。
private int compare(K k1, K k2) {
return (comparator==null ? ((Comparable <K>)k1).compareTo(k2)
: comparator.compare((K)k1, (K)k2));
}
回答by Eugene Ryzhikov
JDK does not have it, however it is called ComparableComparatorand it exists in many frameworks such as Spring, Apache Commons, Hibernateand many others
JDK 没有它,但是它被称为ComparableComparator并且它存在于许多框架中,例如Spring、Apache Commons、Hibernate和许多其他框架
回答by MAK
I think if a class has a natural ordering, it is more usual in Java for it to implement Comparable
rather than have a Comparator
implementation for each class.
我认为如果一个类具有自然顺序,那么在 Java 中更常见的是实现它Comparable
而不是Comparator
每个类都有一个实现。
Thus, if the objects in question have a natural ordering defined, they must implement Comparable
and have the compareTo
method defined. No need to go looking for a Comparator
. Most classes in java.util take either an optional Comparator
if there is any specific ordering to be imposed, or simply try to call compareTo
on the objects if there is no other ordering specified.
因此,如果所讨论的对象定义了自然顺序,则它们必须实现Comparable
并compareTo
定义方法。无需去寻找Comparator
. java.util 中的大多数类要么选择一个可选的,Comparator
如果要强加任何特定的顺序,要么只是尝试调用compareTo
对象,如果没有指定其他顺序。
So, long story short: Implement Comparable
whenever you want to impose a natural ordering on a class, only use a Comparator
when you want something other than the natural ordering.
所以,长话短说:Comparable
只要你想对类强加自然排序,就实现,只有Comparator
当你想要自然排序以外的东西时才使用 a 。
回答by Kevin Bourrillion
Yes, the JDK definitely has it! Here it is:
是的,JDK 绝对有!这里是:
Collections.reverseOrder(Collections.reverseOrder())
Collections.reverseOrder(Collections.reverseOrder())
Just kidding. (But it's true. (Just don't actually use that. (Ever.)))
只是在开玩笑。(但这是真的。(只是不要实际使用它。(永远。)))