Treemap会动态订购吗?
假设我在地图上输入了一个将对象作为钥匙的输入:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你愿意。
treemap
没有可能的方法知道值在地图中的位置应该更改。treemap
没有用这么多单词说出来,但是地图
指定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, butMap
specifies thatand
TreeMap
documents that it uses the specified comparator instead ofequals
.treemap
treemap /a>根据其自然订购或比较器
在施工时提供。仅当元素 添加到地图中时,此行为才适用。
treemap
类在修改条目的键 时不会执行任何重排。实际上,一旦将对象用作地图条目的钥匙,就不应修改该对象,否则相应的值可能会变得无法追踪(取决于
comparecto
和等于
方法已定义)。Elements within a
TreeMap
are stored according to their natural ordering or to theComparator
provided at construction time.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
andequals
methods have been defined).