简体   繁体   English

如何在Java中对Map的键进行排序?

[英]How can I sort the keys of a Map in Java?

This is a very basic question, I'm just not that good with Java. 这是一个非常基本的问题,我对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. 我有一个Map,我想按排序顺序得到一个列表或一些键,所以我可以迭代它们。

Use a TreeMap , which is an implementation of the SortedMap interface. 使用TreeMap ,它是SortedMap接口的实现。 It presents its keys in sorted order. 它按排序顺序显示其键。

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 constructor of TreeMap to create a new map with sorted keys. 如果您正在使用未按照您喜欢的方式排序的另一个Map实现,则可以将其传递给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. TreeMap适用于实现Comparable接口的任何类型的键,将它们置于“自然”顺序中。 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 . 对于不可Comparable键或其自然排序不是您需要的键,您可以实现自己的Comparator并在构造函数中指定它。

You have several options. 你有几个选择。 Listed in order of preference: 按优先顺序列出:

  1. Use a SortedMap : 使用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. 它使键保持排序,因此您无需在迭代之前对它们进行排序。
  2. There is no #2. 没有#2。
  3. There is no #3, either. 也没有#3。
  4. SortedSet<whatever> keys = new TreeSet<whatever>(myMap.keySet());
  5. 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. 最后两个将为您提供您想要的,但只有在您只想迭代一次然后忘记整个事情时才应该使用。

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.
}

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()之后完成任务(比如使用.map(...).forEach(...) ),而不是在列表中收集然后迭代列表。

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

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