Collision resolution in Java HashMap

前端 未结 9 1950
半阙折子戏
半阙折子戏 2020-12-12 12:29

Java HashMap uses put method to insert the K/V pair in HashMap. Lets say I have used put method and now HashMap<

相关标签:
9条回答
  • 2020-12-12 13:23

    It could have formed a linked list, indeed. It's just that Map contract requires it to replace the entry:

    V put(K key, V value)

    Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

    http://docs.oracle.com/javase/6/docs/api/java/util/Map.html

    For a map to store lists of values, it'd need to be a Multimap. Here's Google's: http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Multimap.html

    A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values.

    Edit: Collision resolution

    That's a bit different. A collision happens when two different keys happen to have the same hash code, or two keys with different hash codes happen to map into the same bucket in the underlying array.

    Consider HashMap's source (bits and pieces removed):

    public V put(K key, V value) {
        int hash = hash(key.hashCode());
        int i = indexFor(hash, table.length);
        // i is the index where we want to insert the new element
        addEntry(hash, key, value, i);
        return null;
    }
    
    void addEntry(int hash, K key, V value, int bucketIndex) {
        // take the entry that's already in that bucket
        Entry<K,V> e = table[bucketIndex];
        // and create a new one that points to the old one = linked list
        table[bucketIndex] = new Entry<>(hash, key, value, e);
    }
    

    For those who are curious how the Entry class in HashMap comes to behave like a list, it turns out that HashMap defines its own static Entry class which implements Map.Entry. You can see for yourself by viewing the source code:

    GrepCode for HashMap

    0 讨论(0)
  • 2020-12-12 13:24

    In a HashMap the key is an object, that contains hashCode() and equals(Object) methods.

    When you insert a new entry into the Map, it checks whether the hashCode is already known. Then, it will iterate through all objects with this hashcode, and test their equality with .equals(). If an equal object is found, the new value replaces the old one. If not, it will create a new entry in the map.

    Usually, talking about maps, you use collision when two objects have the same hashCode but they are different. They are internally stored in a list.

    0 讨论(0)
  • 2020-12-12 13:26

    First of all, you have got the concept of hashing a little wrong and it had been rectified by Mr. Sanjay.

    And yes, Java indeed implement a collision resolution technique. When two keys get hashed to a same value (as the internal array used is finite in size and at some point the hashcode() method will return same hash value for two different keys) at this time, a linked list is formed at the bucket location where all the informations are entered as an Map.Entry object that contains a key-value pair. Accessing an object via a key will at worst require O(n) if the entry in present in such a lists. Comparison between the key you passed with each key in such list will be done by the equals() method.

    Although, from Java 8 , the linked lists are replaced with trees (O(log n))

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