Treemap会动态订购吗?

发布于 2025-01-21 13:32:58 字数 267 浏览 0 评论 0原文

假设我在地图上输入了一个将对象作为钥匙的输入:

treeNode.put(someObject,someValue); 

然后一段时间后我将其输入该对象,然后更改对象,以便现在,如果求助于该对象,则将其降落在地图中的其他地方。

treeNode.get(someObject); 
someObject.change();

我是否必须删除地图中的旧条目,然后再次放置以使地图与新钥匙保持一致?

Let's say I put in an entry in to my map which holds an object as a key:

treeNode.put(someObject,someValue); 

then some time later I get that entry, and change the object so that now if resorted it lands somewhere else in the map.

treeNode.get(someObject); 
someObject.change();

Do I have to remove the old entry in the map, and put again for the map to remain consistent with the new key?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星星的軌跡 2025-01-28 13:32:58

是的,你愿意。 treemap没有可能的方法知道值在地图中的位置应该更改。

treemap没有用这么多单词说出来,但是地图指定

如果以equals比较的方式更改对象的值,则未指定地图的行为。

Treemap使用指定比较器而不是等于的文档。

Yes, you do. There is no possible way for TreeMap to know that a value's position in the map should have changed.

TreeMap doesn't say this in so many words, but Map specifies that

The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map.

and TreeMap documents that it uses the specified comparator instead of equals.

娇妻 2025-01-28 13:32:58

treemap treemap /a>根据其自然订购或 比较器 在施工时提供。

根据其键的自然顺序对地图进行排序,或者根据地图创建时间提供的比较器,具体取决于使用哪个构造函数。

仅当元素 添加到地图中时,此行为才适用。 treemap类在修改条目的键 时不会执行任何重排。

实际上,一旦将对象用作地图条目的钥匙,就不应修改该对象,否则相应的值可能会变得无法追踪(取决于comparecto等于方法已定义)。

之所排序地图的立场,等于。

Elements within a TreeMap are stored according to their natural ordering or to the Comparator provided at construction time.

The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

This behavior applies only when an element is added to the map. The TreeMap class does not perform any rearrangement when the entries' keys are modified.

In fact, once an object has been used as a key for a map's entry, this shouldn't be modified, or else the corresponding value might become untraceable (depending on how the compareTo and equals methods have been defined).

This is so because the Map interface is defined in terms of the equals operation, but a sorted map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文