简体   繁体   中英

Sorting technique followed by TreeMap?

当我们尝试打印存储在其中的数据时,谁能解释一下如何在TreeMap自动对数据进行排序?

They aren't; TreeMap uses a red-black tree to manage the data, and this tree implicitly keeps the data sorted. All that the iterator has to do is to traverse the nodes.

物品已经排序-通过使用红黑树在内部存储物品来实现。

If the TreeMap stores objects that implement the Comparable interface (and String does) than it uses the .compareTo method to compare individual Strings and determine the sort order.

On the other hand you can provide a Comparator when constructing the TreeMap, and it will use that object to compare objects and determine the sort order. You can use Comparators to compare objects that do not implement Comparable, or enforce a different sorting strategy. For example, the String.compareTo method performs case-sensitive comparison, but you could provide the String.CASE_INSENSITIVE_ORDER comparator which would result in case-insensitive sorting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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