兩個數組
- bucket數組:存儲key的hash桶,桶指的是把hashcode分配到一定的范圍內
- entry數組:用來存儲實現的值,它是一個單向鏈表,bucket總是存儲鏈表的最后一個元素
實現方式
通過哈希桶來實現的k/v存儲,通過key的hash碼,再進行桶計算,生成一個在某個范圍內的值,這就是桶的索引號,再把值存儲到桶對應的entry里,桶bucket存儲了entry的索引號,通過一個bucket可以直接或者間接找到一個entry.
- 直接找到:當hash沒有沖突時,它存儲的就是真實的entry索引
- 間接找到:當hash出現沖突(碰撞)時,它就會把當前最后的索引賦值這個新entry.next,而新的entry的索引就是現在的bucket的值。
實現流程圖
graph LR key-->hashcode hashcode-->bucket桶運算 bucket桶運算-->得到bucket索引 得到bucket索引-->bucket值就是entry的索引 bucket值就是entry的索引-->x("↓")
graph LR bucket值就是entry的索引-->沖突解決 沖突解決-->單向鏈表next指向上一個值 單向鏈表next指向上一個值-->單身鏈表查找 單身鏈表查找-->返回結果
數組長度為素數
hash桶數全部使用的是質數,因為我們在hash的定義中,hash函數使用的是標准的求模函數,因此這樣定義桶數有利於元素各個桶之間的均勻分布
和減少hash相同值的碰撞概率
。
例如:
舉一個有點極端的例子,假設我們的元素全是偶數1,4,6,8,10,12,14,1,6,18,20,22
如果我們使用4個桶:
0: 4,8,12,16.20
1:
2:6,10,14,18,22
3:
很明顯看出有的桶有很多元素,但是有的桶是空桶,如果我們改為使用3個桶:
0: 6,12,18
1:4,10,16,22
2:2,8,14,20
模擬一個字典的實現
@Getter
@Setter
class KVPair<K, T> {
private K key;
private T value;
private int hashCode;
private int next; //下一個元素的下標索引,如果沒有下一個就為-1
}
/**
* 模擬實現一個字典kv結構.
*
* @param <T>
*/
class MokiHashMap<K, T> {
static int[] primes = {
3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919,
1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591,
17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363, 156437,
187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263,
1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369};
// 桶數組
private int[] buckets;// 最新的entry的索引號,
// 真實的數據
private KVPair<K, T>[] entry; // entry根據next形成一個單鏈表
private int count = 0; // 當前entries的數量
public MokiHashMap() {
buckets = new int[3];
entry = new KVPair[3];
for (int i = 0; i < buckets.length; i++) {
buckets[i] = -1;
}
}
private void reSize() {
int newLength = getPrime(count);
int[] newBuckets = new int[newLength];
for (int i = 0; i < newBuckets.length; i++) {
newBuckets[i] = -1;
}
KVPair<K, T>[] newEntries = new KVPair[newLength];
System.arraycopy(entry, 0, newEntries, 0, count);
System.arraycopy(buckets, 0, newBuckets, 0, count);
entry = newEntries;
buckets = newBuckets;
}
/**
* 得到某個key所在的hash桶
*
* @param key .
* @return
*/
private int getHashBucketIndex(K key) {
int len = buckets.length;
int hashCode = key.hashCode();
int index = hashCode & (len - 1);//len升級的hash桶
return index;
}
/**
* 得到較大的素數.
*
* @param min .
* @return
*/
private int getPrime(int min) {
if (min < 0) {
throw new IllegalArgumentException("最小為3");
}
for (int i = 0; i < primes.length; i++) {
int prime = primes[i];
if (prime > min) return prime;
}
return min;
}
public void add(K key, T value) {
if (count == entry.length) {
reSize();
}
int index = getHashBucketIndex(key);
int entryIndex = buckets[index];
entry[count] = new KVPair();
if (entryIndex < 0) {
entry[count].setNext(-1);
} else {
entry[count].setNext(buckets[index]);
}
entry[count].setHashCode(index);
entry[count].setKey(key);
entry[count].setValue(value);
buckets[index] = count;
count = count + 1;
}
public T find(K key) {
int entryIndex = buckets[getHashBucketIndex(key)];
while (entry[entryIndex].getNext() > -1) {
if (entry[entryIndex].getKey().equals(key)
&& entry[entryIndex].getHashCode() == getHashBucketIndex(key)) {
return entry[entryIndex].getValue();
}
entryIndex = entry[entryIndex].getNext();
}
return null;
}
}
public class KVTest {
@Test
public void testDic() {
MokiHashMap<String, String> dic = new MokiHashMap<>();
dic.add("ok", "1");
dic.add("zzl", "2");
dic.add("lr", "3");
dic.add("dd", "1");
dic.add("a", "b");
dic.add("b", "c");
dic.add("d", "e");
dic.add("e", "f");
System.out.println("dic find:" + dic.find("a"));
}
}