HashMap源码初探

2023-10-26

Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

​ Hash表是基于Map接口的实现。该实现提供了所有的map接口函数,并且允许k,v为null。(除了非同步和允许null外,HashMap类完全的等同于HashTable)。HashMap类不能够保证map映射的顺序;特别是是,不能够映射的顺序持久不变。

上述内容描述了,HashMap允许k,v为null,并且说明了在并发的场景下,HashMap是线程不安全的,容易出现数据的覆盖和丢失

This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the “capacity” of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it’s very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

​ 假设hash函数能够将元素离散的分布在桶中,那么HashMap对于get和put方法的实现时常数级别的。当在遍历HashMap的时候,所需的时间与HashMap的实例(桶的数量capacity)加上键值对的数量(size)成正比。因此初始化的容量即桶的数量不能够太大(或者负载因子不能太小)。

上述内容描述了,HashMap在执行get和put方法的时候,执行时间是常数级别的。同时在初始化HashMap的时候,桶的大小不能够太大,负载因子不能够太小。

An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.

HashMap的实例有两个参数影响其性能:初始化容量(桶的大小)和负载因子。初始化容量是在hash表中桶的数量,初始化容量仅仅是hash表创建时的容量。负载因子是能够判断桶是否满的一个度量标准,如果当前HashMap的size为10,元素个数为5的话,当前的负载因子为0.5,表示已经装了一半的数据。随着hash表中的实体数量超过负载因子和初始化容量的时候,hash表会进行rehash过程,也就是扩容,扩容的大小是当前桶数量的2的幂次方倍,如果当前桶的数量为14,则扩容后数量为32。

上述内容描述了,HashMap的性能由两个因素决定:负载因子和初始化容量。hash扩容的过程。

As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.

一般意义来讲,默认的负载因子为0.75,这个值是对执行时间空间的一个均衡。负载因子越大,证明桶越能被装满,空间利用率很好,但是在查找的时候增加了查找开销(主要体现在get和put方法上)。因此在设置初始化桶容量的时候,需要考虑map中实体的数量的负载因子的大小,这样能够减少reHash过程。如果初始化容量超过最大实体数量除以负载因子,rehash过程将不会发生。也就是说初始化容量为1000的时候,负载因子为0.75,最大实体数量为99*0.75即132的时候,永远不会发生rehash。

上述内容描述了负载因子设置为0.75的原因是基于空间和时间上的tradeoff。至于为什么负载因子越大,get方法查找的时间越长,是因为随着插入的数据增多,越容易发生hash碰撞,发生hash碰撞后,该实体就会存储在链表或者红黑树上,查询的效率急剧下降。

If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table. Note that using many keys with the same hashCode() is a sure way to slow down performance of any hash table. To ameliorate impact, when keys are Comparable, this class may use comparison order among keys to help break ties.

​ 如果有太多的实例要存储在HashMap中,需要创建一个较大数量的桶来进行存储,以便能够根据自适应hash来满足实体的高效映射。使用相同的hashCode()函数对数量较大不同键计算hash会降低hash表的性能。为了改善性能,当key是可比的时候,HashMap可以使用键之间的比较顺序来帮助打破关系。

​ 为何初始化的容量越充足,存储效率更高,是因为不需要进行rehash过程,也即是将数据从一个地方转移到另一个地方。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HashMap源码初探 的相关文章

随机推荐

  • 【Unity】 2D 游戏 库存模块实现

    库存模块主要参考了 youtube 上的视频 BMo 的 Flexible INVENTORY SYSTEM in Unity with Events and Scriptable Objects 和 Simple Inventory UI
  • DVWA SQL injection

    low 猜测表名 1 union select 1 group concat table name from information schema tables where table schema dvwa 如果出现问题 到MySQL里将
  • Java常用对象API——基本数据类型对象包装类

    基本数据类型对象包装类 为了方便操作基本数据类型值 将其封装成了对象 在对象中定义了属性和行为丰富了该数据的操作 用于描述该对象的类就称为基本数据类型对象包装类 byte Byte short Short int Integer long
  • EXCEL中数据透视表的(空白)如何不显示,并且不影响数据更新

    或许有碰到同样问题的 希望对大家有所帮助 1 数据透视表更新过来的数据显示 空白 不好看 开始将用户做了筛选 将空白的复选框去掉 可以达到效果 但是发现有数据更新时 新的数据不能被同步显示 除非手动去再次筛选用户将除空白外的数据勾选 2 点
  • Ble Mesh的Heatbeat(心跳)&地址&Model(模型)

    心跳 将节点配置为定期发送称为心跳消息的消息 Heartbeat 消息的目的 1 表示该节点仍然处于活动状态 2 允许根据传递 Heartbeat 消息所需的跳数确定其与接收者的距离 Heartbaeat的opcode 和Friend re
  • 主存储器的基本组成

    主存储器的基本组成 存储体 存储体也叫存储矩阵 是由一个个存储0或1的记忆单元 存储元 构成的 为了存取存储体中的信息 必须对存储单元进行编址 编址单位是指具有相同地址的那些存储元件构成的一个单位 常见有按字节编址 寻址访存 CPU首先把被
  • C#集合(泛型集合与非泛型)

    每日一句 自律 努力 方法 坚持 时间 优秀 集合特点 一种数据容器 一种数据结构 容纳多个数据 大小可变 空间不一定连续 命名空间 非泛型集合 System Collections 非泛型集合 System Collections Gen
  • 动态博客系统

    Halo 是我折腾过的众多博客系统里面 最好 最容易上手的动态博客系统之一 solo 也是 轻快 简洁 功能强大 正文 上周末正在募集团队一起写算法题 群里讨论需要一个网站来存放文章 恰巧我有一个已经备案但闲置的域名 马上开干 之前的网站是
  • 解惑React之this.setState({ [name]: value })

    react之this setState name value 疑问 学习React中文官方文档中的非空组件与受控组件中 遇到如下代码 class Reservation extends React Component constructor
  • 【面试系列】重排链表

    题意 原题链接 思路 快慢指针找到中点 或者先遍历得到长度 再遍历一半也可行 反转后半部分 归并两部分 代码 Definition for singly linked list struct ListNode int val ListNod
  • Excel 解析,通过Excel的地址和MultipartFile进行解析

    目录 两种方法都用到了read 和getValue 方法对数据进行解析 只是二者传入的Excel数据格式不一样 第一种方法 通过Excel地址进行解析Excel的数据 第二种方法 解析Excel的MultipartFile数据流获取数据 H
  • mmocr 训练字符检测模型

    目录 1 数据集 2 config文件配置 3 测试模型 1 数据集 这里以icdar2015字符检测为例https blog csdn net jizhidexiaoming article details 124149164 spm 1
  • Visio 2007/2010 左侧"形状"窗口管理

    Visio 2007 2010 左侧 形状 窗口管理 Visio 打开后 通常窗口左侧会有一个 形状 面板 我们可以方便地从中选择需要的形状 有时为了获得更大的版面空间或者不小心关闭了形状面板 怎么把它重新调出来 我们可以从 视图 中把它找
  • 使用docker快速搭建服务器环境

    思路 将nginx mysql tomcat等环境打包为一个个docker 然后使用docker compose管理 服务器内安装docker相关环境 然后直接运行docker compose配置 即可快速搭建完成服务器环境 之后可以将相关
  • Markdown / KaTex数学公式汇总

    目录 LaTex和KaTex 软件推荐 Mathpix 一 如何插入公式 二 上下标 三 常用运算符 四 高级运算符 五 常用数学符号 六 特殊符号 6 1 箭头 6 2 公式序号 七 括号使用 八 矩阵 九 集合运算 十 希腊字母 十一
  • 使用反射实现动态修改@Excel的注解属性

    业务场景 我们使用poi实现数据导出时 通常是根据 Excel name xxx 来确定列名 通常情况下这个是不会发生变动的 但这里就说少数情况 在我们需要这里根据某些情况来进行改变的时候 我们就需要用到反射 AirQualityRanki
  • Java反射(自己的理解)

    动态语言 运行是代码可以根据某些条件改变自身结构 像js和php python等 但是我们不像c 是一门静态语言 可以准确的说我们是一门准动态语言 因为反射让我们具有动态性 我来直接用我所理解的反射给大家先讲一下大概 这绝对让你的耳目一新
  • 五、单向散列函数

    单向散列函数 获取消息的指纹 当需要比较两条消息是否一致时 我们不必直接对比消息本身的内容 只要对比它们的 指纹 就可以了 单向散列函数 one wayftnction 有一个输人和一个输出 其中输人称为消息 message 输出称为散列值
  • 全国大学生数字建模竞赛、中国研究生数学建模竞赛(数学建模与计算实验)前言

    1 什么是数学建模 2 所需要学的知识 知识算法分类表格汇总 3 所需要的软件工具 4 论文模板 查找文献 查找数据 一 什么是数学建模 全国大学生数字建模竞赛 National College Student Mathematical M
  • HashMap源码初探

    Hash table based implementation of the Map interface This implementation provides all of the optional map operations and