如何在 Java 中对 Map 的键进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/571388/
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
How can I sort the keys of a Map in Java?
提问by Bialecki
This is a very basic question, I'm just not that good with Java. I have a Map and I want to get a list or something of the keys in sorted order so I can iterate over them.
这是一个非常基本的问题,我只是不太擅长 Java。我有一个 Map,我想按排序顺序获取一个列表或一些键,以便我可以遍历它们。
采纳答案by erickson
Use a TreeMap
, which is an implementation of the SortedMap
interface. It presents its keys in sorted order.
使用 a TreeMap
,它是SortedMap
接口的实现。它按排序顺序显示其键。
Map<String, Object> map = new TreeMap<String, Object>();
/* Add entries to the map in any order. */
...
/* Now, iterate over the map's contents, sorted by key. */
for (Map.Entry<String, ?> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
If you are working with another Map implementation that isn't sorted as you like, you can pass it to the constructorof TreeMap
to create a new map with sorted keys.
如果您正在使用另一个未按您喜欢的方式排序的 Map 实现,您可以将其传递给of的构造函数TreeMap
以创建一个带有排序键的新映射。
void process(Map<String, Object> original) {
Map<String, Object> copy = new TreeMap<String, Object>(original);
/* Now use "copy", which will have keys in sorted order. */
...
}
A TreeMap
works with any type of key that implements the Comparable
interface, putting them in their "natural" order. For keys that aren't Comparable
, or whose natural ordering isn't what you need, you can implement your own Comparator
and specify that in the constructor.
ATreeMap
与实现Comparable
接口的任何类型的键一起使用,将它们按“自然”顺序排列。对于不是的键Comparable
,或者其自然顺序不是您需要的键,您可以实现自己的键Comparator
并在构造函数中指定。
回答by Michael Myers
You have several options. Listed in order of preference:
您有多种选择。按优先顺序列出:
- Use a
SortedMap
:SortedMap<whatever> myNewMap = new TreeMap<whatever>(myOldMap);
This is vastly preferable if you want to iterate more than once. It keeps the keys sorted so you don't have to sort them before iterating. - There is no #2.
- There is no #3, either.
SortedSet<whatever> keys = new TreeSet<whatever>(myMap.keySet());
List<whatever> keys = new ArrayList<whatever>(myMap.keySet()); Collections.sort(keys);
- 使用 a
SortedMap
:SortedMap<whatever> myNewMap = new TreeMap<whatever>(myOldMap);
如果您想迭代不止一次,这是非常可取的。它保持键排序,因此您不必在迭代之前对它们进行排序。 - 没有#2。
- 也没有#3。
SortedSet<whatever> keys = new TreeSet<whatever>(myMap.keySet());
List<whatever> keys = new ArrayList<whatever>(myMap.keySet()); Collections.sort(keys);
The last two will get you what you want, but should only be used if you only want to iterate once and then forget the whole thing.
最后两个将得到你想要的东西,但只有当你只想迭代一次然后忘记整个事情时才应该使用。
回答by Peter Lawrey
You can create a sorted collection when iterating but it make more sense to have a sorted map in the first place. (As has already been suggested)
您可以在迭代时创建一个排序的集合,但首先拥有一个排序的映射更有意义。(正如已经建议的那样)
All the same, here is how you do it.
同样,这就是你如何做到的。
Map<String, Object> map;
for(String key: new TreeSet<String>(map.keySet()) {
// accessed in sorted order.
}
回答by Shreyas
Apart from the methods mentioned in other answers, with Java 8 streams, another shorthand to get a sorted key list from a map would be -
除了其他答案中提到的方法之外,对于 Java 8 流,从地图中获取排序键列表的另一种速记是 -
List<T> sortedKeys = myMap.keySet().stream().sorted().collect(Collectors.toList());
One could actually get stuff done after .sorted()
as well (like using a .map(...)
or a .forEach(...)
), instead of collecting it in the list and then iterating over the list.
实际上也可以在之后完成一些事情.sorted()
(例如使用 a.map(...)
或 a .forEach(...)
),而不是将其收集在列表中然后遍历列表。