前言
- 前面四篇說(shuō)了線性表和鏈表,并且也手寫(xiě)了其中一些的實(shí)現(xiàn)原理,我們先說(shuō)說(shuō)他們的數(shù)據(jù)結(jié)構(gòu)
數(shù)組:它采用了連續(xù)的內(nèi)存存儲(chǔ)空間,對(duì)于指定下標(biāo)的查找,時(shí)間復(fù)雜度尾O(1),通過(guò)給定值進(jìn)行查找,需要將一個(gè)一個(gè)的遍歷,時(shí)間復(fù)雜度為O(n),對(duì)于一般的查找和刪除,數(shù)組元素需要移動(dòng),時(shí)間復(fù)雜度尾O(n)
線性鏈表:而對(duì)于鏈表之間的插入和刪除,僅僅處理鏈表結(jié)點(diǎn)之間的關(guān)系就行,時(shí)間復(fù)雜度尾O(1),但是對(duì)于查找的話,要一個(gè)結(jié)點(diǎn)一個(gè)結(jié)點(diǎn)進(jìn)行對(duì)比,時(shí)間復(fù)雜度尾)(n)
那么有沒(méi)有一種結(jié)構(gòu)既可以支持隨機(jī)訪問(wèn),并且插入和刪除的效果高呢,那么肯定是有的 就是HashMap,它內(nèi)部實(shí)現(xiàn)了一張hash表。我們就來(lái)看看它是怎么玩的
HashMap
特點(diǎn):hash表又叫散列表,是一種非常重要的數(shù)據(jù)結(jié)構(gòu),應(yīng)用場(chǎng)景非常豐富,比如緩存技術(shù)的核心就是內(nèi)部維護(hù)了一張強(qiáng)大的hash表,并且hashmap的實(shí)現(xiàn)原理也是許多面試經(jīng)常問(wèn)的。它對(duì)數(shù)據(jù)的插入、刪除、查找、性能非常的高,不考慮hash沖突的情況下,僅僅只需用hasn函數(shù)算出位置就可以找到要找的元素,那么它內(nèi)部是怎么實(shí)現(xiàn)的 我們接著來(lái)看看。
實(shí)現(xiàn)原理
1、數(shù)據(jù)結(jié)構(gòu)的物理存儲(chǔ)結(jié)構(gòu)有兩種:一種是數(shù)組一種是線性鏈表,而上面說(shuō)的一次性定位,肯定是只有數(shù)組才能做到一次定位元素,沒(méi)錯(cuò)hashmap的主干就是數(shù)組,所以我們要插入或者查找元素,我們可以通過(guò)hash函數(shù)算出下標(biāo),一次性就可以定位到元素,那么我么就可以得到存儲(chǔ)位置=f(x) 這里的f(x) 是可以自定義的,當(dāng)然系統(tǒng)肯定是有人家自己的。
2、hash沖突
然而上面這張圖是有問(wèn)題,假如我們要插入兩個(gè)不同元素,但是我們通過(guò)hash函數(shù)算出兩個(gè)存儲(chǔ)的地址相同咋辦,不用問(wèn),肯定是有這種可能性的,那么這種被稱為hash沖突也叫hash碰撞。所以我們說(shuō)一個(gè)好的hash函數(shù)是非常重要的,他能夠盡量的保證計(jì)算簡(jiǎn)單和散列地址分布均勻。但是我們知道數(shù)組是一片連續(xù)的存儲(chǔ)空間,再好的hash函數(shù)也避免不了碰撞,那么是如何解決呢。
3、hash沖突解決方案
hash沖突解決方案有多種:開(kāi)放定址法(發(fā)生沖突,繼續(xù)尋找下一塊未被占用的存儲(chǔ)地址),在散列函數(shù)法,鏈地址法,而hashmap是用了鏈地址法,也就是數(shù)組+鏈表
4、圖解分析
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;//存儲(chǔ)指向下一個(gè)Entry的引用,單鏈表結(jié)構(gòu)
int hash;//對(duì)key的hashcode值進(jìn)行hash運(yùn)算后得到的值,存儲(chǔ)在Entry,避免重復(fù)計(jì)算
/**
* Creates new entry.
*/
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
上圖還有這個(gè)源碼可以看出,hashmap內(nèi)部是維護(hù)了一個(gè)實(shí)體entry,那么這個(gè)entry就是每個(gè)結(jié)點(diǎn),里面包含了我們存的key和value,還有計(jì)算的hash值,還有下一個(gè)結(jié)點(diǎn),那么它就是
源碼解析
1、先看hashmap幾個(gè)重要的字段
transient int size;實(shí)際存儲(chǔ)的key-value鍵值隊(duì)的個(gè)數(shù)
int threshold;閾值,當(dāng)table為空的時(shí)候,是初始數(shù)量,默認(rèn)為16,當(dāng)table被填充, 一般就是capacity*loadFactory 后面會(huì)說(shuō)到。
final float loadFactor;是一個(gè)負(fù)載因子,代表了table的填充度,默認(rèn)是0.75 一般是0.6到0.9之間最佳
hashmap有4個(gè)構(gòu)造器,看一個(gè)比較重要的構(gòu)造器
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
//此處對(duì)傳入要初始的大小進(jìn)行校驗(yàn)不能大于1<<30 也就是2的30次冪
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " + loadFactor);
this.loadFactor = loadFactor;
threshold = initialCapacity;
init(); //init方法在HashMap中沒(méi)有實(shí)際實(shí)現(xiàn),不過(guò)在其子類如 linkedHashMap中就會(huì)有對(duì)應(yīng)實(shí)現(xiàn)
}
看了半天這構(gòu)造器里面也沒(méi)有創(chuàng)建數(shù)組啊 不要急 hashmap是在put方法才會(huì)創(chuàng)建的 (有一個(gè)構(gòu)造方法會(huì)創(chuàng)建),那么我們就來(lái)看看put方法
public V put(K key, V value) {
//如果table數(shù)組為空數(shù)組{},進(jìn)行數(shù)組填充(為table分配實(shí)際內(nèi)存空間),入?yún)閠hreshold,此時(shí)threshold為initialCapacity 默認(rèn)是1<<4(16)
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
//如果key為null,存儲(chǔ)位置為table[0]或table[0]的沖突鏈上
if (key == null)
return putForNullKey(value);
int hash = sun.misc.Hashing.singleWordWangJenkinsHash(key);//看到?jīng)]這個(gè)函數(shù) 這個(gè)就是系統(tǒng)為我們系統(tǒng)的hash函數(shù)
int i = indexFor(hash, table.length);//獲取在table中的實(shí)際位置
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
//如果該對(duì)應(yīng)數(shù)據(jù)已存在,執(zhí)行覆蓋操作。用新value替換舊value,并返回舊value
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;//保證并發(fā)訪問(wèn)時(shí),若HashMap內(nèi)部結(jié)構(gòu)發(fā)生變化,快速響應(yīng)失敗
addEntry(hash, key, value, i);//新增一個(gè)entry
return null;
}
我們?cè)趤?lái)看看inflateTable這個(gè)方法
private void inflateTable(int toSize) {
int capacity = roundUpToPowerOf2(toSize);//capacity一定是2的次冪
threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);//此處為threshold賦值,取capacity*loadFactor和MAXIMUM_CAPACITY+1的最小值,capaticy一定不會(huì)超過(guò)MAXIMUM_CAPACITY,除非loadFactor大于1
table = new Entry[capacity];
initHashSeedAsNeeded(capacity);
}
inflateTable這個(gè)方法主要是用于為數(shù)組在內(nèi)存中進(jìn)行分配空間的 通過(guò)roundUpToPowerOf2(toSize)可以確保capacity為大于或者等于toSIze的二次冪,比如toSize為13則capacity為16 ,就是通過(guò)下面這個(gè)算法來(lái)實(shí)現(xiàn)的
private static int roundUpToPowerOf2(int number) {
// assert number >= 0 : "number must be non-negative";
return number >= MAXIMUM_CAPACITY
? MAXIMUM_CAPACITY
: (number > 1) ? Integer.highestOneBit((number - 1) << 1) : 1;
}
還有這個(gè)方法是通過(guò)hash值和table.length-1來(lái)& 這樣得到的值永遠(yuǎn)不會(huì)大于數(shù)組的大小,當(dāng)然也可以取模 但是取& 效率更高一點(diǎn)。
/**
* 返回?cái)?shù)組下標(biāo)
*/
static int indexFor(int h, int length) {
return h & (length-1);
}
再來(lái)看看addEntry這個(gè)方法
void addEntry(int hash, K key, V value, int bucketIndex) {
if ((size >= threshold) && (null != table[bucketIndex])) {
resize(2 * table.length);//當(dāng)size超過(guò)臨界閾值threshold,并且即將發(fā)生哈希沖突時(shí)進(jìn)行擴(kuò)容 并且擴(kuò)容的數(shù)量為兩倍的擴(kuò)
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}
createEntry(hash, key, value, bucketIndex);
}
這是createEntry方法
void createEntry(int hash, K key, V value, int bucketIndex) {
HashMapEntry<K,V> e = table[bucketIndex];
//這里就是將Entry插入到bucketindex這個(gè)位置
table[bucketIndex] = new HashMapEntry<>(hash, key, value, e);
size++;
}
那么為什么擴(kuò)容一定是2的次冪呢 我們來(lái)看看resize這個(gè)方法(擴(kuò)容是一個(gè)非常消耗資源的操作,所以平時(shí)我們可以預(yù)估一下我們的hashmap存多少數(shù)據(jù),調(diào)用它的設(shè)置大小的構(gòu)造方法 這以前我也是不知道。。。。)
void resize(int newCapacity) {
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
Entry[] newTable = new Entry[newCapacity];
transfer(newTable, initHashSeedAsNeeded(newCapacity));
table = newTable;
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
再來(lái)看看transfer這個(gè)方法
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;//for循環(huán)中的代碼,逐個(gè)遍歷數(shù)組,重新計(jì)算索引位置,將老數(shù)組數(shù)據(jù)復(fù)制到新數(shù)組中去(數(shù)組不存儲(chǔ)實(shí)際數(shù)據(jù),所以僅僅是拷貝引用而已)
for (Entry<K,V> e : table) {
while(null != e) {
Entry<K,V> next = e.next;
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
int i = indexFor(e.hash, newCapacity); //將當(dāng)前entry的next鏈指向新的索引位置,newTable[i]有可能為空,有可能也是個(gè)entry鏈,如果是entry鏈,直接在鏈表頭部插入。
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
這個(gè)方法是將老數(shù)組中的數(shù)據(jù)逐個(gè)遍歷,復(fù)制到新的擴(kuò)容后的數(shù)組,數(shù)組的索引位置的計(jì)算是通過(guò)key值通過(guò)hash函數(shù)運(yùn)行后和length-1進(jìn)行&運(yùn)算得到的。
hasnMap的數(shù)組長(zhǎng)度要保持2的次冪,比如16的二進(jìn)制表示為100000,呢么length-1就是15,二進(jìn)制是01111,同理擴(kuò)容后的數(shù)組長(zhǎng)度為32,二進(jìn)制1000000,length-1為31 二進(jìn)制為011111,從下圖我們也能看出,這樣能保證最低位都是1,而擴(kuò)容后只有一位差異,也就是多出了最左位的1,這樣在通過(guò)h&(length-1) 的時(shí)候,只要h對(duì)應(yīng)的最左邊的哪一個(gè)差一位是0,這樣就能保證得到的新數(shù)組索引和老數(shù)組索引一致(減少了之前已經(jīng)散列的老數(shù)組的數(shù)據(jù)位置重新調(diào)換)我是這么理解的
還有,數(shù)組的長(zhǎng)度保持2的此冪,length-1的低位都是1,會(huì)使得獲得的數(shù)組索引index更加均勻
再來(lái)看看get方法
public V get(Object key) { //如果key為null,則直接去table[0]處去檢索即可。
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key);
return null == entry ? null : entry.getValue();
}
看看getEntry
final Entry<K,V> getEntry(Object key) {
if (size == 0) {
return null;
}
//通過(guò)key的hashcode值計(jì)算hash值
int hash = (key == null) ? 0 : sun.misc.Hashing.singleWordWangJenkinsHash(key);
//indexFor (hash&length-1) 獲取最終數(shù)組索引,然后遍歷鏈表,通過(guò)equals方法比對(duì)找出對(duì)應(yīng)記錄
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
- hashMap源碼就分析到這里了 里面重要的方法和邏輯結(jié)構(gòu)也就這些了 至于 刪除和修改也都是 在上面分析的源碼有體現(xiàn)
手寫(xiě)hashMap實(shí)現(xiàn)
public class HashMapDemo<K, V> {
//默認(rèn)的數(shù)組的大小
private int DEFAULT_CAPACITY = 16;
private int length;//數(shù)組中真實(shí)存儲(chǔ)的大小
private HashMapDemoEntity<K, V> mTable[] = new HashMapDemoEntity[DEFAULT_CAPACITY];
public void put(K k, V v) {
if (k == null) {//這里就不支持存null為key了
return;
}
int hash = getHash(k);
int index = hash & length;//得到的是一個(gè)位置
HashMapDemoEntity<K, V> entity = mTable[index];
if (entity != null) {
//這里證明該數(shù)組下標(biāo)處存了值
HashMapDemoEntity<K, V> newEntity = new HashMapDemoEntity<>(k, v, hash, entity);
//然后將
mTable[index] = newEntity;
//這是鏈表里面的 但是這里寫(xiě)的是存的值是可以唯一的
}
addEntity(index, hash, k, v);
}
public V get(K k) {
if (k==null){
return null;
}
HashMapDemoEntity<K,V> entry = getEntry(k);
return null == entry ? null : entry.getValue();
}
private HashMapDemoEntity<K, V> getEntry(K key) {
int hash = key.hashCode();
for (HashMapDemoEntity<K,V> e = mTable[hash&length];e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
private void addEntity(int index, int hash, K k, V v) {
//這里添加涉及到擴(kuò)容
HashMapDemoEntity<K, V> e = mTable[index];
mTable[index] = new HashMapDemoEntity<>(k, v, hash, e);
}
public int getHash(K k) {
return k.hashCode();//hasn函數(shù) 得到一個(gè)hash值
}
/**
* 這個(gè)就是一個(gè)內(nèi)部維護(hù)的靜態(tài)內(nèi)部類
*
* @param <K>
* @param <V>
*/
private static class HashMapDemoEntity<K, V> {
K key;
V value;
int hash;
HashMapDemoEntity<K, V> next;
public HashMapDemoEntity(K k, V v, int h, HashMapDemoEntity<K, V> n) {
this.key = k;
this.value = v;
this.hash = h;
this.next = n;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}
}