> For the complete documentation index, see [llms.txt](https://jupiter-1992.gitbook.io/jupiter-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jupiter-1992.gitbook.io/jupiter-note/hou-duan/gao-xiao-bian-cheng/05hashmap-pai-xu.md).

# 05 HashMap 排序

HashMap 的值是没有顺序的，它是按照 key 的 HashCode 来实现的，对于这个无序的 HashMap 我们要怎么来实现排序呢？

## 根据 key 排序

TreeMap 基于红黑树实现，该映射根据其键的自然顺序进行排序，或者根据创建映射时提供的 Comparator 进行排序。Comparator 可以对集合对象或者数组进行排序的比较器接口。

```java
Map<String, String> map = new TreeMap<>(Comparator.naturalOrder());
map.put("b", "bbb");
map.put("d", "ddd");
map.put("c", "ccc");
map.put("a", "aaa");

Set<String> keySet = map.keySet();
for (String key : keySet) {
    System.out.println(key + ":" + map.get(key));
}
```

```
a:aaa
b:bbb
c:ccc
d:ddd
```

## 根据 value 排序

对 value 排序我们就需要借助于 Collections 的 `sort(List<T> list, Comparator<? super T> c)` 方法，该方法根据指定比较器产生的顺序对指定列表进行排序。但是有一个前提条件，那就是所有的元素都必须能够根据所提供的比较器来进行比较。

```java
HashMap<String, Integer> countMap = new HashMap<>();
// 升序比较器
Comparator<Map.Entry<String, Integer>> valueComparator = Comparator.comparingInt(Map.Entry::getValue);
// map 转换成 list
List<Map.Entry<String, Integer>> list = new ArrayList<>(countMap.entrySet());
// 排序
list.sort(valueComparator);
for (Map.Entry<String, Integer> entry : list) {
    System.out.println(entry.getKey() + ":" + entry.getValue());
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jupiter-1992.gitbook.io/jupiter-note/hou-duan/gao-xiao-bian-cheng/05hashmap-pai-xu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
