两个键是相同的 hashMap over write”现有的?
哈希映射如何在内部存储数据...我知道它会计算键的 HashCode 值并将其存储。如果两个键具有相同的哈希码,它将放入同一个桶中。但是为什么如果“两个键是相同的 hashMap 覆盖”现有的呢?
How exactly hash map store data internally ... I knew it will calculate HashCode value of key and store it.If two key having same hash code it will put into same bucket. But why if "two keys are same hashMap over write" the existing one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嗯,这就是它的设计目的。它是键/值对的映射,其中任何键都与 0 或 1 个值关联。如果您为某个键
输入
第二个值,则该键的条目将被替换。但它不仅仅基于哈希码 - 它也会测试键的相等性。两个键可以不相等,但具有相同的哈希码。重要的是两个相等的键必须具有相同的哈希码。
如果您想为单个键存储多个值,您应该使用类似 Guava 的 多图。
Well, that's what it's designed to do. It's a mapping of key/value pairs, where any key is associated with 0 or 1 value. If you
put
a second value for a key, the entry for that key will be replaced.It's not based just on the hash code though - it will test the keys for equality, too. Two keys can be unequal but have the same hash code. The important thing is that two equal keys must have the same hash code.
If you want to store multiple values for a single key, you should use something like Guava's Multimap.
如果
hashCode()
相同,则不会覆盖该值。仅当它们等于 等于方法。It will not overwrite the value if the
hashCode()
is same. It will overwrite only if they are equal by the equals method.请参阅 http://en.wikipedia.org/wiki/Hash_table 和 http://www.docjar.com/html/api/java/util/HashMap.java .html
哈希表或哈希映射是一个链表数组,以哈希码为键。
See http://en.wikipedia.org/wiki/Hash_table and http://www.docjar.com/html/api/java/util/HashMap.java.html
A hash table or hash map is an array of linked lists, keyed by hashcode.
Hashcode 代码的主要目的是减少基于哈希的集合中 equals 方法的调用次数。相同的哈希码不需要为 equals 方法返回 true。但如果你说它的 equals 是 true,那么它的 hascode 应该是 true。
Hashcode code main purpose is to reduce the number of invocation of equals method in the hash based collection. Same hash code need not return true for equals method. But if you say its equals is true, then it hascode should be true.
哈希函数通常用于消除重复数据。这就是集合类型的原因
就像 Hashmap 不允许存储重复数据。
该算法也已用于数据库中,以消除检索时可能的重复项。
Hash functions are generallyy used for eliminating duplicate data.That is why collections type
like Hashmap not allowing to store duplicate data.
This algorithms has been used in database as well to eliminate possible duplicates while retrieval.