Internals of how the HashMap put() and get() methods work (basic logic only )

后端 未结 3 1850
面向向阳花
面向向阳花 2020-12-06 07:48

When we put a key instance say \"key\" and a Value instance say \"value\" in a HashMap class using put() method , what does the HashMap

3条回答
  •  伪装坚强ぢ
    2020-12-06 08:14

    If you talk about higher picture it is just like below.Here i refer item as a key of Map

    While Putting items.

    1. Calculate hashcode of key
    2. If basket with that hashcode is present then use the equals method on the key search the keys i that basket to determine if the element is to be added or replace.
    3. If not there then create new basket (rehashing) and add that element to that.

    Get:

    1. Get the hashcode of key
    2. Go to that basket
    3. Iterate using equals on the key will return you that element from that basket.

提交回复
热议问题