# TreeMap的儲存方式
[參考網站](https://www.itread01.com/content/1549254784.html)
**Question:**
```java=
//Given the code fragment:
Map<Integer, String> books = new TreeMap<>();
books.put (1007, "A");
books.put (1002, "C");
books.put (1001, "B");
books.put (1003, "B");
System.out.println (books);
```
## What is the result?
A. {1007 = A, 1002 = C, 1001 = B, 1003 = B}
**B. {1001 = B, 1002 = C, 1003 = B, 1007 = A}**
C. {1002 = C, 1003 = B, 1007 = A}
D. {1007 = A, 1001 = B, 1003 = B, 1002 = C}
- [x] **Answer: B**
:::info
TreeMap 是一個有序的key-value集合,它是通過紅黑樹實現的。
TreeMap基於紅黑樹(Red-Black tree)實現。該對映根據其***鍵的自然順序進行排序***,或者根據建立對映時提供的 Comparator 進行排序,具體取決於使用的構造方法。
---
TreeMap(Comparator<? super K> comparator)
:::