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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:01:10  来源:igfitidea点击:

What is 'natural ordering' in a TreeMap?

javatreemap

提问by John Threepwood

Possible Duplicate:
How can I sort the keys of a Map in Java?

可能的重复:
如何在 Java 中对 Map 的键进行排序?

In class TreeMapthe 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 Comparableinterface, 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 TreeMapthat has a Kthat does not implement Comparable(Unless you explicitly provide a Comparatorvia the TreeMapconstructor).

如果您要自己尝试此操作,您会发现不能使用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 Comparableinterface

用于“自然排序”的TreeMap 的 javadocs链接将您带到Comparable界面

回答by dasblinkenlight

"Natural" ordering is the ordering implied by the implementation of the Comparableinterface 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 Comparableinterface in the class(es) used as keys to TreeMap, or
  • Supply an implementation of the Comparatorthat 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 TreeMapsource. Line 541, for instance.

如有疑问,请阅读TreeMap来源。例如,第 541 行。

回答by GOTO 0

Natural ordering is just the order provided by the Comparableinterface. You can create a TreeMapwithout 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 是一个要求。