Collision resolution in Java HashMap

前端 未结 9 1949
半阙折子戏
半阙折子戏 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:05

    When you insert the pair (10, 17) and then (10, 20), there is technically no collision involved. You are just replacing the old value with the new value for a given key 10 (since in both cases, 10 is equal to 10 and also the hash code for 10 is always 10).

    Collision happens when multiple keys hash to the same bucket. In that case, you need to make sure that you can distinguish between those keys. Chaining collision resolution is one of those techniques which is used for this.

    As an example, let's suppose that two strings "abra ka dabra" and "wave my wand" yield hash codes 100 and 200 respectively. Assuming the total array size is 10, both of them end up in the same bucket (100 % 10 and 200 % 10). Chaining ensures that whenever you do map.get( "abra ka dabra" );, you end up with the correct value associated with the key. In the case of hash map in Java, this is done by using the equals method.

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

    There is no collision in your example. You use the same key, so the old value gets replaced with the new one. Now, if you used two keys that map to the same hash code, then you'd have a collision. But even in that case, HashMap would replace your value! If you want the values to be chained in case of a collision, you have to do it yourself, e.g. by using a list as a value.

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

    There is difference between collision and duplication. Collision means hashcode and bucket is same, but in duplicate, it will be same hashcode,same bucket, but here equals method come in picture.

    Collision detected and you can add element on existing key. but in case of duplication it will replace new value.

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

    Your case is not talking about collision resolution, it is simply replacement of older value with a new value for the same key because Java's HashMap can't contain duplicates (i.e., multiple values) for the same key.

    In your example, the value 17 will be simply replaced with 20 for the same key 10 inside the HashMap.

    If you are trying to put a different/new value for the same key, it is not the concept of collision resolution, rather it is simply replacing the old value with a new value for the same key. It is how HashMap has been designed and you can have a look at the below API (emphasis is mine) taken from here.

    public V put(K key, V value)

    Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.


    On the other hand, collision resolution techniques comes into play only when multiple keys end up with the same hashcode (i.e., they fall in the same bucket location) where an entry is already stored. HashMap handles the collision resolution by using the concept of chaining i.e., it stores the values in a linked list (or a balanced tree since Java8, depends on the number of entries).

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

    When multiple keys end up in same hash code which is present in same bucket. When the same key has different values then the old value will be replaced with new value.

    Liked list converted to balanced Binary tree from java 8 version on wards in worst case scenario.

    Collision happen when 2 distinct keys generate the same hashcode() value. When there are more collisions then there it will leads to worst performance of hashmap.

    Objects which are are equal according to the equals method must return the same hashCode value. When both objects return the same has code then they will be moved into the same bucket.

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

    It isn't defined to do so. In order to achieve this functionality, you need to create a map that maps keys to lists of values:

    Map<Foo, List<Bar>> myMap;
    

    Or, you could use the Multimap from google collections / guava libraries

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