What happens to the lookup in a Hashmap or Hashset when the objects Hashcode changes

后端 未结 4 454
太阳男子
太阳男子 2021-01-04 10:00

In a Hashmap the hash code of the key provided is used to place the value in the hashtable. In a Hashset the obects hashcode is used to place the value in the underlying has

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-04 10:55

    With Java's hashes, the original reference is simply not found. It's searched in the bucket corresponding the current hashcode, and not found.

    To recover from this after the fact, the Hash keySet must be iterated over, and and any key which is not found by contains method must be removed through the iterator. Preferable is to remove the key from the map, then store the value with new key.

提交回复
热议问题