没有合适的资源?快使用搜索试试~ 我知道了~
java软件技术文档-深入java8的集合3:HashMap的实现原理.pdf
1 下载量 195 浏览量
2022-11-17
17:24:07
上传
评论
收藏 467KB PDF 举报
温馨提示
试读
20页
java软件技术文档
资源推荐
资源详情
资源评论
深入 java8 的集合 3:HashMap 的实现原理
一、概述
二话不说,一上来就点开源码,发现里面有一段介绍如下:
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.
翻译一下大概就是在说,这个哈希表是基于 Map 接口的实现的,它允许 null 值和
null 键,它不是线程同步的,同时也不保证有序。
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.
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.
不要急,我真的不是来翻译的。再来看看这一段,讲的是 Map 的这种实现方式为 get
(取)和 put(存)带来了比较好的性能。但是如果涉及到大量的遍历操作的话,就
尽量不要把 capacity 设置得太高(或 load factor 设置得太低),否则会严重降低遍
历的效率。
影响 HashMap 性能的两个重要参数:“initial capacity”(初始化容量)和”load
factor“(负载因子)。简单来说,容量就是哈希表桶的个数,负载因子就是键值对
个数与哈希表长度的一个比值,当比值超过负载因子之后,HashMap 就会进行 rehash
操作来进行扩容。
HashMap 的大致结构如下图所示,其中哈希表是一个数组,我们经常把数组中的每
一个节点称为一个桶,哈希表中的每个节点都用来存储一个键值对。在插入元素时,
如果发生冲突(即多个键值对映射到同一个桶上)的话,就会通过链表的形式来解
决冲突。因为一个桶上可能存在多个键值对,所以在查找的时候,会先通过 key 的
哈希值先定位到桶,再遍历桶上的所有键值对,找出 key 相等的键值对,从而来获
取 value。
(因为我这里主要介绍的是 Java 对 HashMap 的实现,而不是 hash 算法,所以如果
对哈希算法不了解,建议先去学习一下!)
二、属性
再来看看 HashMap 类中包含了哪些重要的属性,这对下面介绍 HashMap 方法的实
现有一定的参考意义。
//默认的初始容量为 16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
//最大的容量上限为 2^30
static final int MAXIMUM_CAPACITY = 1 << 30;
//默认的负载因子为 0.75
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//变成树型结构的临界值为 8
static final int TREEIFY_THRESHOLD = 8;
//恢复链式结构的临界值为 6
static final int UNTREEIFY_THRESHOLD = 6;
//哈希表
transient Node<K,V>[] table;
//哈希表中键值对的个数
transient int size;
//哈希表被修改的次数
transient int modCount;
//它是通过 capacity*load factor 计算出来的,当 size 到达这个值时,
就会进行扩容操作
int threshold;
//负载因子
剩余19页未读,继续阅读
资源评论
悠闲饭团
- 粉丝: 151
- 资源: 3303
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功