java TreeMap 中的“自然排序”是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14087575/
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
What is 'natural ordering' in a TreeMap?
提问by John Threepwood
Possible Duplicate:
How can I sort the keys of a Map in Java?
可能的重复:
如何在 Java 中对 Map 的键进行排序?
In class TreeMap
the Java API says:
在课堂上TreeMap
,Java API 说:
A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
基于红黑树的 NavigableMap 实现。映射根据其键的自然顺序进行排序,或者通过映射创建时提供的 Comparator 进行排序,具体取决于使用的构造函数。
What is meant by natural ordering ? A class used as key does not have to implement the Comparable
interface, but what ordering will be used instead ?
自然排序是什么意思?用作键的类不必实现Comparable
接口,而是使用什么顺序?
采纳答案by Brian Roach
If you were to try this yourself you'd find that you cannot use a TreeMap
that has a K
that does not implement Comparable
(Unless you explicitly provide a Comparator
via the TreeMap
constructor).
如果您要自己尝试此操作,您会发现不能使用TreeMap
具有K
未实现的 a Comparable
(除非您Comparator
通过TreeMap
构造函数明确提供 a )。
public class App
{
public static void main( String[] args )
{
TreeMap<App,String> tm = new TreeMap<App,String>();
tm.put(new App(), "value");
}
}
Exception in thread "main" java.lang.ClassCastException: App cannot be cast to java.lang.Comparable
线程“main”中的异常 java.lang.ClassCastException:应用程序无法转换为 java.lang.Comparable
The javadoc for put()
states this explicitly:
的 javadocput()
明确说明了这一点:
Throws:
ClassCastException - if the specified key cannot be compared with the keys currently in the map
抛出:
ClassCastException - 如果指定的键不能与映射中的当前键进行比较
The link in javadocs for TreeMapfor "Natural Ordering" takes you to the Comparable
interface
用于“自然排序”的TreeMap 的 javadocs链接将您带到Comparable
界面
回答by dasblinkenlight
"Natural" ordering is the ordering implied by the implementation of the Comparable
interface by the objects used as keys in the TreeMap
. Essentially, RBTree must be able to tell which key is smaller than the other key, and there are two ways to supply that logic to the RBTree implementation:
“自然”排序是Comparable
由用作TreeMap
. 本质上,RBTree 必须能够分辨哪个键小于另一个键,并且有两种方法可以将该逻辑提供给 RBTree 实现:
- Implement
Comparable
interface in the class(es) used as keys toTreeMap
, or - Supply an implementation of the
Comparator
that would do comparing outside the key class itself.
Comparable
在用作键的类中实现接口TreeMap
,或- 提供
Comparator
将在关键类本身之外进行比较的实现。
回答by James
It isa requirement to implement Comparable
. It's just not enforced at compile time.
这是实施的要求Comparable
。它只是在编译时没有强制执行。
jamlong% cat Wah.java
import java.util.*;
public class Wah {
public static void main(String[] args) {
TreeMap<Wah, Integer> wah = new TreeMap<Wah, Integer>();
wah.put(new Wah(), 1);
wah.put(new Wah(), 2);
}
}
jamlong% java Wah
Exception in thread "main" java.lang.ClassCastException: Wah cannot be cast to java.lang.Comparable
at java.util.TreeMap.put(TreeMap.java:542)
at Wah.main(Wah.java:8)
When in doubt, read the TreeMap
source. Line 541, for instance.
如有疑问,请阅读TreeMap
来源。例如,第 541 行。
回答by GOTO 0
Natural ordering is just the order provided by the Comparable
interface. You can create a TreeMap
without a Comparator
, but then any attempt to put any keys that do not implement a natural order will throw a ClassCastException
.
自然排序只是Comparable
接口提供的顺序。您可以创建TreeMap
不带 a 的 a Comparator
,但是任何尝试放置任何未实现自然顺序的键都会抛出 a ClassCastException
。
回答by duffymo
The natural ordering is determined by the keys.
自然顺序由键决定。
So if you use a String you'll get one ordering; Integer will give another.
所以如果你使用 String 你会得到一个排序;整数会给另一个。
I think Comparable is a requirement.
我认为 Comparable 是一个要求。