简体   繁体   English

什么时候对TreeMap和ConcurrentSkipListMap进行排序?

[英]When are TreeMap and ConcurrentSkipListMap sorted?

I have a Map for which I provide a key class for which I've implemented a comparitor. 我有一个Map,为此我提供了一个已实现比较器的键类。 The comparitor is partly time-based, so the order may change as time marches on. 比较器部分基于时间,因此顺序可能会随着时间的流逝而改变。 The question is: when is the comparison between objects done, at insertion time into the map or at iterator creation time? 问题是:在插入地图时或在创建迭代器时,何时进行对象之间的比较?

The tree is built on insert. 该树建立在插入上。

Also, be aware of the following warning in the javadoc of Map : 另外,请注意Map的javadoc中的以下警告:

Note: great care must be exercised if mutable objects are used as map keys. 注意:如果将可变对象用作地图键,则必须格外小心。 The behavior of a map is not specified if the value of a [key] object is changed in a manner that affects equals comparisons while the object is a key in the map. 如果[key]对象的值以影响equals比较的方式更改 ,而该对象是映射中的键, 则不会指定映射的行为

So, the compare method should be stable, ie should not change because of the progression of time. 因此,比较方法应该是稳定的,即不应随时间的推移而改变。

Any time a key changes, it must be removed before the change, and re-added after the change. 每当密钥更改时,必须在更改之前将其删除,并在更改之后重新添加。

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

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