Java - HashMap confusion about collision handling and the get() method

前端 未结 5 1847
说谎
说谎 2021-01-13 15:28

I\'m using a HashMap and I haven\'t been able to get a straight answer on how the get() method works in the case of collisions.

Let\'s say

5条回答
  •  抹茶落季
    2021-01-13 16:04

    The documentation for Hashmap.put() clearly states, "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"

    If you would like to have a list of objects associated with a key, then store a list as the value.

    Note that 'collision' generally refers to the internal working of the HashMap, where two keys have the same hash value, not the use of the same key for two different values.

提交回复
热议问题