【发布时间】:2015-03-22 09:01:02
【问题描述】:
如何获取与键关联的值?
假设,我的代码如下:
Hashmap hm = new Hashmap();`
hm.put("a","aValue"); // Suppose hashcode created for key "a" is 209
hm.put("b","bValue"); // Here hashcode created for key "b" is 209 as well.
现在,我想检索与键 "b" 关联的值。我会打电话给hm.get("b")。因为,hashmap 根据键的哈希码搜索键/值对。 Hashmap 将为键 "b" 找到 209 哈希码。由于为键 "a" 找到了相同的哈希码,因此 Hashmap 可能返回值 "aValue" 而不是预期值 "bValue"
所以,这是我的问题,我想检索与键关联的值。我将如何做到这一点?
【问题讨论】:
标签: java collections hashmap