简体   繁体   English

为什么我的TreeMap没有排序?

[英]Why is my TreeMap not sorting?

I used a TreeMap where the key is a String and the value is of type Integer . 我使用了TreeMap ,其中键是String ,值的类型是Integer When I output the Map object, it's not printing in sorted order. 当我输出Map对象时,它不按排序顺序打印。

Here's the code I used: 这是我使用的代码:

TreeMap<String, Integer> m = new TreeMap<String, Integer>();
m.put("Hello", 1);
m.put("world", 2);
m.put("Zertt", 5);
m.put("Hello", 1);
m.put("world", 2);
System.out.println("map : " + m);

I expect the output to be sorted like this : 我希望输出像这样排序:

map : {Hello=1, world=2, Zertt=5} map:{Hello = 1,world = 2,Zertt = 5}

But instead I get this : 但相反,我得到了这个:

map : {Hello=1, Zertt=5, world=2} map:{Hello = 1,Zertt = 5,world = 2}

The natural ordering of String s is case sensitive, so Z comes before w (all upper case letters come before all lower case letters). String s的自然顺序区分大小写,因此Zw之前出现(所有大写字母都在小写字母之前)。

Use 使用

TreeMap<String, Integer> m = new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);

for case insensitive order. 对于不区分大小写的顺序。

Javadoc says : Javadoc说:

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. 地图根据其的自然顺序进行排序,或者根据使用的构造函数在地图创建时提供的比较器进行排序。

EDIT : Eran's answer is right, String ordering is case sensitive by default. 编辑:Eran的答案是正确的,字符串排序默认情况下区分大小写。

As answered before string natural order is case sensitive. 如前所述,字符串自然顺序区分大小写。 But, if you want insentive ordering, you can provide comparator as TreeMap constructor parameter: 但是,如果你想要insentive排序,你可以提供比较器作为TreeMap构造函数参数:

Map<String, Integer> m = new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);

ps Notice, when using case insentive order keys will compare insentive too: ps注意,当使用案例因果顺序键时也会比较insentive:

m.put("Hello", 1);
m.put("helLo", 6);

Result is 6 and key is Hello 结果是6,关键是Hello

Maybe this information will be helpful. 也许这些信息会有所帮助。

In the class TreeMap contains constructors: 在类TreeMap中包含构造函数:

  1. TreeMap () TreeMap()

  2. TreeMap (Comparator comp) TreeMap(比较器comp)

  3. TreeMap (Map m) TreeMap(地图m)

  4. TreeMap (SortedMap sm) TreeMap(SortedMap sm)

The first constructor creates a collection in which all the elements are sorted in natural order of their keys. 第一个构造函数创建一个集合,其中所有元素按其键的自然顺序排序。

The second constructor creates an empty collection, the elements of which will be sorted according to the law, which is defined in the transmission comparator. 第二个构造函数创建一个空集合,其元素将根据法律进行排序,该法则在传输比较器中定义。

The third constructor creates a TreeMap based on an existing Map. 第三个构造函数基于现有Map创建TreeMap。

The fourth constructor creates a TreeMap based on existing SortedMap, elements of which will be sorted according to the law transmitted SortedMap. 第四个构造函数基于现有的SortedMap创建一个TreeMap,其元素将根据法律传输的SortedMap进行排序。

Note that keys used for the sorting, rather than the value. 注意用于排序的键,而不是值。

树形图中的排序基于键的自然顺序而不是值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM