HashMap的源码分析
hashMap的底层实现是 数组+链表 的数据结构,数组是一个Entry<K,V>[] 的键值对对象数组,在数组的每个索引上存储的是包含Entry的节点对象,每个Entry对象是一个单链表结构,维护这下一个Entry节点的引用;有点绕,用个图来展示吧:

Entry<K,V>[] 数组部分保存的是首个Entry节点;Entry节点包含一个 K值引用 V值引用 以及 引用下一个Entry 节点的next引用;
Entry节点的java代码实现如下:
static class Entry<K,V> implements Map.Entry<K,V> {
final K key; //key 引用
V value; //value 引用
Entry<K,V> next; //下一个Entry 节点的引用
}
下面再看下HashMap 对象的java实现代码:
包含的属性有:
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
{ /**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 /**
* The maximum capacity, used if a higher value is implicitly specified
* by either of the constructors with arguments.
* MUST be a power of two <= 1<<30.
*/
static final int MAXIMUM_CAPACITY = 1 << 30; /**
* The load factor used when none specified in constructor.
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f; /**
* An empty table instance to share when the table is not inflated.
*/
static final Entry<?,?>[] EMPTY_TABLE = {}; /**
* The table, resized as necessary. Length MUST Always be a power of two.
*/
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE; /**
* The number of key-value mappings contained in this map.
*/
transient int size; /**
* The next size value at which to resize (capacity * load factor).
* @serial
*/
// If table == EMPTY_TABLE then this is the initial capacity at which the
// table will be created when inflated.
int threshold; /**
* The load factor for the hash table.
*
* @serial
*/
final float loadFactor;
}
比较重要的属性是:
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE; 表明这是一个 Entry<K,V>[] 的数组类型;
下面看其无参的构造器:
public HashMap() {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}
进入以下的构造方法:
public HashMap(int initialCapacity, float loadFactor) { //initialCapacity:16 loadFactor 0.75f
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY) //MAXIMUM_CAPACITY 1073741824 false
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor)) false
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor); this.loadFactor = loadFactor; //赋值给loadFactor=0.75
threshold = initialCapacity; //赋值给threshold=16 当为16是自动扩容
init();
}
下面再看看put(E e)的方法:
public V put(K key, V value) { //如插入 key="city" value="shanghai"
if (table == EMPTY_TABLE) { //true
inflateTable(threshold); //参数为16
}
if (key == null) // false
return putForNullKey(value);
int hash = hash(key); //返回key值的hash码; 比如返回为 337
int i = indexFor(hash, table.length); //将hash 取模与16 获得的结果为 1
for (Entry<K,V> e = table[i]; e != null; e = e.next) { //遍历 Entry[1] 中的链表节点对象 包含 原先有的节点和新增进去的节点
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { //当Entry中包含 相同的hash码的key 并且key和要添加的key相等即可以是否重复 则进入以下逻辑:新节点替换重复的节点
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
下面是 inflateTable(threshold) 方法的源码;
/**
* Inflates the table.
*/
private void inflateTable(int toSize) { //toSize 16
// Find a power of 2 >= toSize
int capacity = roundUpToPowerOf2(toSize); //capacity=16 threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1); // threshold=16*0.75
table = new Entry[capacity]; //创建 Entry[] 数组长度为16
initHashSeedAsNeeded(capacity); //这个方法可以暂时不用深究
}
下面是 roundUpToPowerOf2(int i) 源码
private static int roundUpToPowerOf2(int number) { // number=16
// assert number >= 0 : "number must be non-negative";
return number >= MAXIMUM_CAPACITY //fase 返回 16
? MAXIMUM_CAPACITY
: (number > 1) ? Integer.highestOneBit((number - 1) << 1) : 1; //number=16>1 返回 16
}
put 方法的源码分析完了之后,接下来再看一下get(Object key) 的方法; 源码:
public V get(Object key) { //如 key="name"
if (key == null) //false
return getForNullKey();
Entry<K,V> entry = getEntry(key);
return null == entry ? null : entry.getValue();
}
final Entry<K,V> getEntry(Object key) { //key=name
if (size == 0) { //false
return null;
}
int hash = (key == null) ? 0 : hash(key); //例如 返回hash=337
for (Entry<K,V> e = table[indexFor(hash, table.length)]; //indexFor(hash, table.length)上面分析过 返回值为 1;遍历 table[1] 中的节点
e != null;
e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k)))) //如果存在key的hash码相等,并且对象也相等则返回 对应的Entry 节点
return e;
}
return null; //否则返回null
}
到此,hashMap 的源码基本分析完毕了,通过源码分析我们知道HashMap的底层是 数组+链表结构来存数数据的,添加节点存储的位置是根据 key 取hash值 再取模于数组长度:返回的数值就是Entry接在在数组的哪个位置;这种方式的存储方式减少了存储的时间和空间的复杂度;
知道了hashMap是由 数组+链表 的数据结构存储数据后,我们也很容易明白hashMap 的遍历方式:
HashMap的源码分析的更多相关文章
- HashMap的源码分析与实现 伸缩性角度看hashmap的不足
本文介绍 1.hashmap的概念 2.hashmap的源码分析 3.hashmap的手写实现 4.伸缩性角度看hashmap的不足 一.HashMap的概念 HashMap可以将其拆分为Hash散列 ...
- Java——HashMap底层源码分析
1.简介 HashMap 根据键的 hashCode 值存储数据,大多数情况下可以直接定位到它的值,因而具有很快的访问速度,但遍历顺序却是不确定的. HashMap 最多只允许一条记录的key为 nu ...
- Java中HashMap的源码分析
先来回顾一下Map类中常用实现类的区别: HashMap:底层实现是哈希表+链表,在JDK8中,当链表长度大于8时转换为红黑树,线程不安全,效率高,允许key或value为null HashTable ...
- HashMap方法源码分析
本文将分析put(),resize(),get()和remove()方法的源码 putval()方法 大致步骤:计算key的hash值:根据hash值计算数组下标:判断下标处是否有节点,无节点则直接插 ...
- Java源码——HashMap的源码分析及原理学习记录
学习HashMap时,需要带着这几个问题去,会有很大的收获: 一.什么是哈希表 二.HashMap实现原理 三.为何HashMap的数组长度一定是2的次幂? 四.重写equals方法需同时重写hash ...
- HashMap LinkedHashMap源码分析笔记
MapClassDiagram
- Java HashMap实例源码分析
引言 HashMap在键值对存储中被经常使用,那么它到底是如何实现键值存储的呢? 一 Entry Entry是Map接口中的一个内部接口,它是实现键值对存储关键.在HashMap中,有Entry的实现 ...
- HashMap的源码分析(一)
1.hashMap的关键值 DEFAULT_INITIAL_CAPACITY:默认初始容量16,∈(0,1<<30),实际大小为2的整数次幂: DEFAULT_LOAD_FACTOR:默认 ...
- HashMap从源码分析数据结构
1. HashMap在链表中存储的是键值对 2. 数组是一块连续的固定长度的内存空间,再好的哈希函数也不能保证得到的存储地址绝对不发生冲突.那么哈希冲突如何解决呢?哈希冲突的解决方案有多种:开放定址法 ...
随机推荐
- test case VS test scenario
---恢复内容开始--- 1. test case: how to test --如何测试 test scenario: what to be tested --测试什么 2. test scen ...
- 微信小程序---人脸识别(wx.startFacialRecognitionVerify)
1.由于人脸核验功能涉及到用户的敏感.隐私信息,因此调用此接口的业务方,需要满足一定的条件,申请小程序的人脸识别api.开通小程序后台的接口权限入口后,开发者可以登录mp.weixin.qq.com小 ...
- redis、mysql、mongdb的比较
特点: 1-1 MySQL:1. 使用c和c++编写,并使用了多种编译器进行测试,保证源代码的可移植性2. 支持多种操作系统3. 为多种编程语言提供可API4. 支持多线程,充分利用CPU资源优化的S ...
- MAC book 无法删除普通用户的解决办法
1来自苹果官网 macOS Sierra: 删除用户或群组 如果您是管理员,当您不想再让某些用户访问 Mac 时,可以删除他们.您也可以删除不想要的群组. 删除用户时,您可以存储该用户的个人文件夹(包 ...
- 以太坊Inner Transaction合约内充值转账
- Android抓取log日志过滤
前提:Android SDK已安装并配置环境变量 1.手机USB调试模式打开,连接PC 2.cmd窗口,执行adb logcat >log.log // 输出日志到一个log文件 或者执行a ...
- Eigen使用矩阵作为函数参数
1 使用矩阵作为函数参数介绍 文章来源Writing Functions Taking %Eigen Types as Parameters Eigen为了在函数中传递不同的类型使用了表达式模板技术. ...
- iis 发布mvc
转载地址:https://www.cnblogs.com/Leo_wl/p/3866625.html
- spring与junit整合测试
1.导包4+2+aop+test 2.配置注解 3.测试
- (PMP)第8章-----项目质量管理
过程质量管理,成果质量的管理 戴明理论:PDCA,戴明环 朱兰理论:质量规划,质量控制,质量改进,朱兰三部曲 克鲁斯比理论:零缺陷,质量免费 石川理论:质量圈,因果图,质量管理七大工具:核对表,帕累托 ...