简体   繁体   English

如果两个不同的对象具有相同的哈希码,会发生什么?

[英]What happens if two different objects have the same hashcode?

It is my understanding that two unequal objects can have the same hashcode. 据我所知,两个不相等的对象可以具有相同的哈希码。 How would this be handled when adding or retrieving from a HashMap java? 在从HashMap java添加或检索时如何处理?

They will just be added to the same bucket and equals() will be used to distinguish them. 它们将被添加到同一个桶中, equals()将用于区分它们。 Each bucket can contain a list of objects with the same hash code. 每个存储桶都可以包含具有相同哈希码的对象列表。

In theory you can return the same integer as a hash code for any object of given class, but that would mean that you loose all performance benefits of the hash map and, in effect, will store objects in a list. 从理论上讲,您可以为给定类的任何对象返回与哈希代码相同的整数,但这意味着您将失去哈希映射的所有性能优势,并且实际上会将对象存储在列表中。

In HashMap, keys along with their associative values are stored in a linked list node in the bucket and the keys are essentially compared in hashmap using equals() method not by hashcode. 在HashMap中,键及其关联值存储在存储桶中的链接列表节点中,并且基本上使用equals()方法而不是hashcode在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.
  • If a.equals(b) returns true , bValue will replace aValue and bValue will be returned. 如果a.equals(b)返回true ,则bValue将替换aValue并返回bValue
  • If a.equals(b) returns false , another node will be created in the bucket list, so when you call get("b") you will get bValue since a.equals(b) is false . 如果a.equals(b)返回false ,则将在桶列表中创建另一个节点,因此当您调用get("b")您将获得bValue因为a.equals(b)false

HashMap is working on the concept of hashing and indexing. HashMap正在研究散列和索引的概念。 Internally HashMap stores values in Array of Nodes. 内部HashMap将值存储在节点数组中。 Each node behaves as LinkedList. 每个节点都表现为LinkedList。

Each node of linked list have 4 values : 链表的每个节点都有4个值:

  1. int hash
  2. K key
  3. V value
  4. Node<K, V> next

HashMap Internal structure: HashMap内部结构:

HashMap内部结构图像

While inserting the value in HashMap, first hashcode of Key is generated and based on some Algorithm it will calculate the index. 在HashMap中插入值时,会生成Key的第一个哈希码,并根据某些算法计算索引。

So our value will store in specific index with hashcode, key, value and address of next element. 因此,我们的值将使用下一个元素的哈希码,键,值和地址存储在特定索引中。

While retrieving the value from HashMap, first hashcode will generate and then index(same way as at the time of insertion). 从HashMap中检索值时,第一个哈希码将生成然后索引(与插入时的方式相同)。 While getting the value from index, first it will check for hashcode, if hashcode will match then only it will check for key from Node by using equals method. 在从索引获取值时,首先它将检查哈希码,如果哈希码匹配,那么只有它将使用equals方法从Node检查密钥。 If key will match then only it will return the value or else it will check for next Node with same hashcode. 如果key匹配,则只返回该值,否则它将检查具有相同哈希码的下一个Node。

在这种情况下,您可以使用IdentityHashMap,其中具有相同散列的不同对象基于其身份被视为不同。

When two unequal objects have the same hash value, this causes a collision in the hash table, because both objects want to be in the same slot (sometimes called a bucket). 当两个不相等的对象具有相同的哈希值时,这会导致哈希表中的冲突,因为两个对象都希望位于同一个槽中(有时称为桶)。 The hash algorithm must resolve such collisions. 哈希算法必须解决此类冲突。 Reaching back into fading memories of my college algorithms course, I remember three basic ways of doing this: 回到我大学算法课程的褪色记忆中,我记得三种基本方法:

  1. Look for the next empty slot in the hash table and put the object there. 查找哈希表中的下一个空槽并将对象放在那里。 Pros: easy to implement, cons: can lead to clustering of objects and degrade performance, capacity may be exceeded 优点:易于实现,缺点:可能导致对象聚集并降低性能,可能会超出容量
  2. Have a secondary hash function to use when there's a conflict: Pros: usually fast, cons: must write a second hash function and may still get collisions, and capacity may be exceeded 在发生冲突时使用辅助哈希函数:优点:通常很快,缺点:必须编写第二个哈希函数并且仍然可能发生冲突,并且可能会超出容量
  3. Make a linked-list of objects from the conflicted slot of the hash table. 从哈希表的冲突插槽中创建对象的链接列表。 Pros/cons: usually fast for decent hash function and load factors, but can degrade to linear search in worst case 优点/缺点:通常快速获得合适的散列函数和负载因子,但在最坏的情况下会降级为线性搜索

I think the Java hash classes use the third method, but they might use a combination approach. 我认为Java哈希类使用第三种方法,但它们可能使用组合方法。 The key to good hashing though is to make sure the hash table has a big enough capacity and to write good hash functions. 良好哈希的关键是确保哈希表具有足够大的容量并编写好的哈希函数。 A hash table that only has as many buckets as objects it is holding will probably have conflicts. 只有与它所持有的对象一样多的存储桶的哈希表可能会有冲突。 Usually you want the hash table to be about twice as big as the number of objects it stores. 通常,您希望哈希表大约是它存储的对象数量的两倍。 Java's HashMap will grow as needed, but you can give it a starting capacity and load factor if you want. Java的HashMap将根据需要增长,但如果需要,可以为其提供启动容量和负载因子。

The hash function is up to the programmer. 哈希函数取决于程序员。 You could just return 0 for all objects, but that will mean the hashing (both storage and retrieval) will become O(n) instead of O(1) ... or in lay terms, it will be dog slow. 您可以为所有对象返回0,但这意味着散列(存储和检索)将变为O(n)而不是O(1)...或者在非专业术语中,它将是狗慢。

Reference : http://www.coderanch.com/t/540275/java/java/objects-hashcode-HashMap-retrieve-objects 参考: http//www.coderanch.com/t/540275/java/java/objects-hashcode-HashMap-retrieve-objects

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果两个相同的对象具有不同的哈希码,那么缺点是什么 - What is the drawback if two same objects have different hashcode 在什么条件下两个不同的对象可能具有相同的hashcode()值..? - Under what Conditions two different objects may have same hashcode() value..? 如果两个对象具有相同的哈希码,那么它们的内存地址呢? - if two objects have same hashcode then what about their memory addresses 如果 hashcode() 是根据 object 的地址创建 hashcode 的,那么两个内容相同的不同对象如何创建相同的 hashcode? - If the hashcode() creates hashcode based on the address of the object, how can two different objects with same contents create the same hashcode? 当两个不同的注释具有相同的名称时会发生什么? - What happens when two different annotations have the same name? 具有相同属性值的不同对象可以在 Java 中具有相同的哈希码吗 - Can different objects with same value for the attributes have same hashcode in Java 为什么具有相同数据的两个不同的 HashSet 具有相同的 HashCode? - Why do two different HashSets with the same data have the same HashCode? 在Java中使用什么集合来存储具有相同哈希码的多个对象? - What collection to use in java for storing multiple objects that have the same hashcode? 具有相同哈希码的两个不相等的对象 - two unequal objects with same hashcode 长度不同的两个字符串可以具有相同的哈希码吗? - Can two strings of different length have the same hashcode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM