HashMap原理学习

概述

HashMap对于做Java的小伙伴来说太熟悉了。估计你们每天都在使用它。它为什么叫做HashMap?它的内部是怎么实现的呢?为什么我们使用的时候很多情况都是用String作为它的key呢?带着这些疑问让我们来了解HashMap!

HashMap介绍

1、介绍

HashMap是一个用”KEY”-“VALUE”来实现数据存储的类。你可以用一个”key”去存储数据。当你想获得数据的时候,你可以通过”key”去得到数据。所以你可以把HashMap当作一个字典。 那么HashMap的名字从何而来呢?其实HashMap的由来是基于Hasing技术(Hasing),Hasing就是将很大的字符串或者任何对象转换成一个用来代表它们的很小的值,这些更短的值就可以很方便的用来方便索引、加快搜索。

在讲解HashMap的存储过程之前还需要提到一个知识点
我们都知道在Java中每个对象都有一个hashcode()方法用来返回该对象的 hash值。HashMap中将会用到对象的hashcode方法来获取对象的hash值。

2、关系

图1展示了HashMap的类结构关系。

图1

HashMap继承了AbstractMap,并且支持序列化和反序列化。由于实现了Clonable接口,也就支持clone()方法来复制一个对象。今天主要说HashMap的内部实现,这里就不对序列化和clone做讲解了。

3、内部介绍

HashMap内部实现原理图

上面的图很清晰的说明了HashMap内部的实现原理。就好比一个篮子,篮子里装了很多苹果,苹果里包含了自己的信息和另外一个苹果的引用

1、和上图显示的一样,HashMap内部包含了一个Entry类型的数组table, table里的每一个数据都是一个Entry对象。

2、再来看table里面存储的Entry类型,Entry类里包含了hashcode变量,key,value 和另外一个Entry对象。为什么要有一个Entry对象呢?其实如果你看过linkedList的源码,你可能会知道这就是一个链表结构。通过我找到你,你再找到他。不过这里的Entry并不是LinkedList,它是单独为HashMap服务的一个内部单链表结构的类。

3、那么Entry是一个单链表结构的意义又是什么呢?在我们了解了HashMap的存储过程之后,你就会很清楚了,接着让我们来看HashMap怎么工作的。

HashMap的存储过程

下面分析一段代码的HashMap存储过程。(这里只是作为演示的例子,并没有真实的去取到了Hash值,如果你有需要可以通过Debug来得到key的Hash值)

1 HashMap hashMap = new HashMap();//line1 2 hashMap.put("one","hello1");//line2 3 hashMap.put("two","hello2");//line3 4 hashMap.put("three","hello3");//line4 5 hashMap.put("four","hello4");//line5 6 hashMap.put("five","hello5");//line6 7 hashMap.put("six","hello6");//line7 8 hashMap.put("seven","hello7");//line8

put操作的伪代码可以表示如下:

1public V put(K key, V value){ 2 int hash = hash(key); 3 int i = indexFor(hash, table.length); 4 //在table\[i\]的地方添加一个包含hash,key,value信息的Entry类。 5}

下面我们来看上面代码的过程
1、line1创建了一个HashMap,所以我们来看构造函数

1/** 2 \* Constructs an empty <tt>HashMap</tt> with the default initial capacity 3 \* (16) and the default load factor (0.75). 4 */ 5 public HashMap() { 6 this(DEFAULT\_INITIAL\_CAPACITY, DEFAULT\_LOAD\_FACTOR); 7 }

空构造函数调用了它自己的另一个构造函数,注释说明了构建了一个初始容量的空HashMap,那我们就来看它另外一个构造函数。

1public HashMap(int initialCapacity, float loadFactor) { 2 if (initialCapacity < 0) 3 throw new IllegalArgumentException("Illegal initial capacity: " + 4 initialCapacity); 5 if (initialCapacity > MAXIMUM_CAPACITY) 6 initialCapacity = MAXIMUM_CAPACITY; 7 if (loadFactor <= 0 || Float.isNaN(loadFactor)) 8 throw new IllegalArgumentException("Illegal load factor: " + 9 loadFactor); 10 11 this.loadFactor = loadFactor; 12 threshold = initialCapacity; 13 init(); 14 } 15 16void init() { 17 }

上面的代码只是简单的给loadFactor(其实是数组不够用来扩容的)和threshold(内部数组的初始化容量),init()是一个空方法。所以现在数组table还是一个空数组。

1 /** 2 \* An empty table instance to share when the table is not inflated. 3 */ 4 static final Entry<?,?>\[\] EMPTY_TABLE = {}; 5 6 /** 7 \* The table, resized as necessary. Length MUST Always be a power of two. 8 */ 9 transient Entry<K,V>\[\] table = (Entry<K,V>\[\]) EMPTY_TABLE;

2、接下来到了line2的地方, hashMap.put(“one”,”hello1”);在这里先提一下put方法源码:

1public V put(K key, V value) { 2 if (table == EMPTY_TABLE) { 3 inflateTable(threshold);//如果是空的,加载 4 } 5 if (key == null) 6 return putForNullKey(value); 7 int hash = hash(key);获取hash值 8 int i = indexFor(hash, table.length);生成索引 9 for (Entry<K,V> e = table\[i\]; e != null; e = e.next) { 10 Object k; 11 //遍历已存在的Entry,如果要存入的key和hash值都一样就覆盖。 12 if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { 13 V oldValue = e.value; 14 e.value = value; 15 e.recordAccess(this); 16 return oldValue; 17 } 18 } 19 20 modCount++; 21 //添加一个节点 22 addEntry(hash, key, value, i); 23 return null; 24 }

源码很简单,先判断table如果是空的,就初始化数组table,接着如果key是null就单独处理。否则的话就得到key的hash值再生成索引,这里用了indexFor()方法生成索引是因为:hash值一般都很大,是不适合我们的数组的。来看indexFor方法

1/** 2 \* Returns index for hash code h. 3 */ 4 static int indexFor(int h, int length) { 5 // assert Integer.bitCount(length) == 1 : "length must be a non-zero power of 2"; 6 return h & (length-1); 7 }

就是一个&操作,这样返回的值比较小适合我们的数组。

继续 line2put操作,因为开始table是空数组,所以会进入 inflateTable(threshold)方法,其实这个方法就是出实话数组容量,初始化长度是16,这个长度是在开始的构造方法赋值的。
所以,现在空数组变成了长度16的数组了,就像下图一样。
这里写图片描述

接着由于我们的key不为null,到了获取hash值和索引,这里假设int hash = hash(key)和int i = indexFor(hash, table.length)生成的索引i为hash=2306996,i = 4;那么就会在table索引为4的位置新建一个Entry,对应的代码是addEntry(hash, key, value, i);到此结果如下图:
这里写图片描述

新建的Entry内部的变量分别是,hash,key,value,和指向下一节点的next Entry。

3、继续来看line3,line3和line2一样,而且数组不为空直接hash(key)和index。所以直接看图了
这里写图片描述

4、到了line4,这里line4情况有点特殊,我们假设line4里key生成的hashcode产生的index也为4,比如hash(“three”) 的值 63281940
hash&(15)产生的index为4。这种情况由于之前的位置已经有Entry了,所以遍历Entry如果key和hashcode都相同,就直接替换,否则新添加一个Entry,来看一下对应源码

1public V put(K key, V value) { 2 ...//一些代码 3 for (Entry<K,V> e = table\[i\]; e != null; e = e.next) { 4 Object k; 5 if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { 6 V oldValue = e.value; 7 e.value = value; 8 e.recordAccess(this); 9 return oldValue; 10 } 11 } 12 //for循环里判断如果hash和key都一样直接替换。 13 14 modCount++; 15 addEntry(hash, key, value, i);//没有重复的话就addEntry 16 return null; 17 }

上面代码先判断是否需要替换,不需要就调用了addEntry方法。来看addEntry

1void addEntry(int hash, K key, V value, int bucketIndex) { 2 if ((size >= threshold) && (null != table\[bucketIndex\])) { 3 resize(2 * table.length); 4 hash = (null != key) ? hash(key) : 0; 5 bucketIndex = indexFor(hash, table.length); 6 }//判断数组容量是否足够,不足够扩容 7 8 createEntry(hash, key, value, bucketIndex); 9 }

里面又调用了createEntry

1void createEntry(int hash, K key, V value, int bucketIndex) { 2 Entry<K,V> e = table\[bucketIndex\]; 3 table\[bucketIndex\] = new Entry<>(hash, key, value, e); 4 size++; 5 //获取当前节点,然后新建一个含有当前hash,key,value信息的一个节点,并且该节点的Entry指向了前一个Entry并赋值给table\[index\],成为了最新的节点Entry,同时将size加1。 6 }

到这里相信大家很清楚了。来看看图:
这里写图片描述

5、到这里之后的代码都在上面的分析情况当中。我就不一一画图了,直接给出程序执行到最后的图
line5到line8

| 代码 | hashcode | index | key | value | next | | --- | :-: | --: | --- | --- | --- | | hashMap.put(“four”,”hello4”); | 54378290 | 9 | four | hello4 | null | | hashMap.put(“five”,”hello5”); | 39821723 | 8 | five | hello5 | null | | hashMap.put(“six”,”hello6”); | 86726537 | 4 | six | hello6 | line4产生的Entry | | hashMap.put(“seven”,”hello7”); | 28789082 | 2 | seven | hello7 | line3产生的Entry |

结果图如下:
这里写图片描述

到此put 操作就结束了,再来看看取

HashMap的取值过程

我们通过hashMap.get(K key) 来获取存入的值,key的取值很简单了。我们通过数组的index直接找到Entry,然后再遍历Entry,当hashcode和key都一样就是我们当初存入的值啦。看源码:

1 public V get(Object key) { 2 if (key == null) 3 return getForNullKey(); 4 Entry<K,V> entry = getEntry(key); 5 6 return null == entry ? null : entry.getValue(); 7 }

调用getEntry(key)拿到entry ,然后返回entry的value,来看getEntry(key)方法

1final Entry<K,V> getEntry(Object key) { 2 if (size == 0) { 3 return null; 4 } 5 6 int hash = (key == null) ? 0 : hash(key); 7 for (Entry<K,V> e = table\[indexFor(hash, table.length)\]; 8 e != null; 9 e = e.next) { 10 Object k; 11 if (e.hash == hash && 12 ((k = e.key) == key || (key != null && key.equals(k)))) 13 return e; 14 } 15 return null; 16 }

按什么规则存的就按什么规则取,获取到hash,再获取index,然后拿到Entry遍历,hash相等的情况下,如果key相等就知道了我们想要的值。

再get方法中有null的判断,null取hash值总是0,再getNullKey(K key)方法中,也是按照遍历方法来查找的。

到这你肯定明白了为什么HashMap可以用null做key。

了解的存储取值过程和内部实现,其它的方法自己看看源码很好理解,在此就不一一解释了。

几个问题

问题1、HashMap是基于key的hashcode的存储的,如果两个不同的key产生的hashcode一样取值怎么办?
看了上面的分析,你肯定知道,再数组里面有链表结构的Entry来实现,通过遍历所有的Entry,比较key来确定到底是哪一个value;

问题2、HashMap是基于key的hashcode的存储的,如果两个key一样产生的hashcode一样怎么办?
在put操作的时候会遍历所有Entry,如果有key相等的则替换。所以get的时候只会有一个

问题3、我们总是习惯用一个String作为HashMap的key,这是为什么呢?其它的类可以做为HashMap的key吗?
这里因为String是不可以变的,并且java为它实现了hashcode的缓存技术。我们在put和get中都需要获取key的hashcode,这些方法的效率很大程度上取决于获取hashcode的,所以用String的原因:1、它是不可变的。2、它实现了hashcode的缓存,效率更高。如果你对String不了解可以看:Java你可能不知道的事-String

问题4、可变的对象能作为HashMap的key吗?
可变的对象是可以当做HashMap的key的,只是你要确保你可变变量的改变不会改变hashcode。比如以下代码

1public class TestMemory { 2 3 public static void main(String\[\] args) { 4 HashMap hashMap = new HashMap(); 5 TestKey testKey = new TestKey(); 6 testKey.setAddress("sdfdsf");//line3 7 hashMap.put(testKey,"hello"); 8 testKey.setAddress("sdfsdffds");//line5 9 System.out.println(hashMap.get(testKey)); 10 } 11} 12 13public class TestKey { 14 String name; 15 String address; 16 17 public String getName() { 18 return name; 19 } 20 21 public void setName(String name) { 22 this.name = name; 23 } 24 25 public String getAddress() { 26 return address; 27 } 28 29 public void setAddress(String address) { 30 this.address = address; 31 } 32 33 @Override 34 public int hashCode() { 35 if (name==null){ 36 return 0; 37 } 38 return name.hashCode(); 39 } 40}

上面的代码line3到line5对象里的address做了改变,但是由于hashCode是基于name来生成的,name没变,所以依然能够正常找到value。但是如果把setAdress换成name,get就会返回null。这就是为什么我们选择String的原因。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )