简体   繁体   English

如何对地图的键进行排序

[英]How to sort a key of a map

How to sort (any kind of sorting) a key of a map(treemap or hashmap) 如何对地图(树形图或哈希图)的键进行排序(任何排序)

i have a problem and it goes like this. 我有一个问题,它是这样的。

i have a map that has a key of 我有一张地图,钥匙是

27527-683, 27527-683,
27525-1179, 27525-1179,
27525-1571, 27525-1571,
27525-1813, 27525-1813,
27525-4911, 27525-4911,
27526-1303, 27526-1303,
27526-3641, 27526-3641,
27525-3989, 27525-3989,
27525-4083, 27525-4083,
27525-4670, 27525-4670,
27526-4102, 27526-4102,
27526-558, 27526-558,
27527-2411, 27527-2411,
27527-4342 27527-4342

this is the list of keys and the value for each of the key is a list. 这是键的列表,每个键的值是一个列表。
now, how can i sort this key in ascending order by number. 现在,我如何才能按数字升序将此键排序。

ex. 例如 if i want to sort : 1,2,11,20,31,3,10 如果我想排序:1,2,11,20,31,3,10
i want to have as output is : 1,2,3,10,11,20,31 我想作为输出是:1,2,3,10,11,20,31
but when i use the autosort of treemap the output goes : 1,10,11,2,20,3,31 但是当我使用树状图的自动排序时,输出为:1,10,11,2,20,3,31

how can i sort it in ascending order by numeric? 如何按数字升序排列?
please help me. 请帮我。 i can't think of anymore ways because this is my first time handling map and list 我再也想不出办法了,因为这是我第一次处理地图和清单

Your keys are Strings. 您的键是字符串。 The natural ordering of strings is lexicographical. 字符串的自然排序是词典顺序。 You either need to specify a custom comparator in the constructor of the TreeMap, or use an Integer key. 您需要在TreeMap的构造函数中指定一个自定义比较器,或者使用一个Integer键。

Furthermore, you can better represent a Map<Key, List<Value>> as a Google Guava Multimap , see for example SortedSetMultimap . 此外,您可以更好地将Map<Key, List<Value>>为Google Guava Multimap ,例如,参见SortedSetMultimap

Continuing with the Guava example: 继续以番石榴为例:

Multimap<Integer, Person> multimap = SortedSetMultimap.create(Ordering.natural(), Ordering.arbitrary());
multimap.put(1, x);
multimap.put(1, y);
multimap.put(2, z);
multimap.put(1, a);

Then 然后

multimap.get(1) will return a set containing [x, y, a] in some arbitrary order. multimap.get(1)将以任意顺序返回包含[x,y,a]的集合。 multimap.keys() will return a sorted set of [1, 2]. multimap.keys()将返回[1,2]的排序集。

使用SortedMap ,例如java.util.TreeMap

keyword : SortedMap 关键字: SortedMap

SortedMap<Integer, Integer> s = new TreeMap<Integer, Integer>();

s.put(1, 1);
s.put(4, 2);
s.put(2, 3);
System.out.println(s); //{1=1, 2=3, 4=2}

also look at this example 也看这个例子

Your keys are currently Strings, that you would appear to rather handle like Integers. 您的键当前是字符串,看起来像Integers一样处理。 You will need to reformulate how you handle the map object, as many people here have suggested (it may be the correct method). 正如此处许多人所建议的那样,您将需要重新制定如何处理地图对象的方法(这可能是正确的方法)。 An easier method involves using that list of keys you have copied us in on. 一种更简单的方法是使用您复制了我们的密钥列表。

I see there is a way to output the list of keys. 我看到有一种输出键列表的方法。 Sort that however you like. 随便排序即可。 Arrays and lists are easily sorted by built in commands from your runtime environment. 通过运行时环境中的内置命令可以轻松地对数组和列表进行排序。 You might have to first split those elements at the hyphen. 您可能必须先在连字符处拆分这些元素。

I'd recommend a book on Data Structures. 我会推荐一本关于数据结构的书。

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

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