HashMap put()和get()方法如何工作的内部机制(仅限基本逻辑) [英] Internals of how the HashMap put() and get() methods work (basic logic only )

查看:153
本文介绍了HashMap put()和get()方法如何工作的内部机制(仅限基本逻辑)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用 put()在 HashMap 类中放置一个关键实例时,将key和一个Value实例声明为value code>方法, HashMap 类在内部是做什么的。当我们说 hashMap.get(key)

编辑:我不想在这里找到细节,基本上试图理解更大的图像和 equals() hashcode() code> put() get()操作中的code方法。

解决方案

如果你谈论更高级的图片,它就像下面这样。在这里​​我把item称为 of 地图



放置物品时


  1. 计算密钥 hashcode
  2. 如果篮子 code>存在 hashcode ,然后使用 equals 方法在关键字上搜索关键字i以确定元素是否被添加或替换。

  3. 如果不存在,那么创建新的篮子(重新哈哈哈哈)并且添加那个元素。

  4. ol>

    获取:


    1. 获取钥匙的哈希码

    2. 转到那个篮子
    3. 在键上使用 equals >迭代将返回该元素。


    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 class do internally . How does it retrieve the value back when we say hashMap.get(key) ?

    Edit: I do not want details here , basically trying to understand the bigger picture and the role of equals() and hashcode() method in put() and get() operations.

    解决方案

    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.

    这篇关于HashMap put()和get()方法如何工作的内部机制(仅限基本逻辑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆