How does Java HashMap store entries internally

前端 未结 3 534
生来不讨喜
生来不讨喜 2020-12-10 15:00

Say you have a key class (KeyClass) with overridden equals, hashCode and clone methods. Assume that it has 2 primitive fields, a String (name) and an int (id).

Now y

3条回答
  •  萌比男神i
    2020-12-10 15:23

    HashMap maintains a table of entries, with references to the associated keys and values, organized according to their hash code. If you mutate a key, then the hash code will change, but the entry in HashMap is still placed in the hash table according to the original hash code. That's why map.get(keyOriginal) will return null.

    map.keySet() just iterates over the hash table, returning the key of each entry it has.

提交回复
热议问题