【发布时间】:2016-04-12 18:05:46
【问题描述】:
示例
LinkedHashMap<Long, String> myHashMap = new LinkedHashMap<>();
myHashMap.put(new Long(1), "A Value");
问题
- 密钥是参考还是副本?
- 如果我写
String aValue = myHashMap.get(new Long(1));,我会得到"A Value"吗?还是我只是查询了一个不同的对象(参考),因此我会得到一个错误?
【问题讨论】:
-
你尝试的时候发生了什么?
HashMap的 javadoc 是怎么说的? -
javadoc 的第 5 段(“如果有很多映射...”)说了一些关于
hashCode()的内容,但我无法完全得到我需要的答案。可以指出我需要阅读/关注的部分吗? -
@sargas 你的问题没有提到
hashCode。hashCode用于确定存储条目的数组的索引。如果过多的条目具有相同的hashCode,则会降低性能。 -
@PaulBoddington 正确。我刚刚在Map Interface documentation 中找到了我想要的东西。谢谢。
标签: java collections hashmap