What happens if two different objects have the same hashcode?

后端 未结 5 1497
余生分开走
余生分开走 2020-12-28 17:23

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?

相关标签:
5条回答
  • 2020-12-28 17:49

    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. 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.

    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.

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

    0 讨论(0)
  • 2020-12-28 17:52

    They will just be added to the same bucket and equals() will be used to distinguish them. 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.

    0 讨论(0)
  • 2020-12-28 17:59

    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.

    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.
    • 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.
    0 讨论(0)
  • 2020-12-28 18:01

    HashMap is working on the concept of hashing and indexing. Internally HashMap stores values in Array of Nodes. Each node behaves as LinkedList.

    Each node of linked list have 4 values :

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

    HashMap Internal structure:

    While inserting the value in HashMap, first hashcode of Key is generated and based on some Algorithm it will calculate the index.

    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). 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. If key will match then only it will return the value or else it will check for next Node with same hashcode.

    0 讨论(0)
  • 2020-12-28 18:05

    In that case you could use IdentityHashMap, where different objects with same hash are considered as different based on their identities.

    0 讨论(0)
提交回复
热议问题