简体   繁体   English

排序技术之后是TreeMap?

[英]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. TreeMap使用一棵红黑树来管理数据,该树隐式地保持数据的排序。 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. 如果TreeMap存储实现Comparable接口(而String则实现)的对象,则它使用.compareTo方法比较单个String并确定排序顺序。

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. 另一方面,在构造TreeMap时可以提供一个Comparator,它将使用该对象比较对象并确定排序顺序。 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. 例如,String.compareTo方法执行区分大小写的比较,但是您可以提供String.CASE_INSENSITIVE_ORDER比较器,这将导致不区分大小写的排序。

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

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