Hashtable原始碼淺析
阿新 • • 發佈:2018-12-13
Hashtable是併發安全的hash表,結構是陣列+連結串列。
put
// 新增 key value public synchronized V put(K key, V value) { // Make sure the value is not null if (value == null) { throw new NullPointerException(); } // Makes sure the key is not already in the hashtable. Entry<?,?> tab[] = table; // 計算應該在陣列中新增的索引位置 int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; @SuppressWarnings("unchecked") Entry<K,V> entry = (Entry<K,V>)tab[index]; // 遍歷連結串列,如果已經存在相同的key,則新值覆蓋舊值 for(; entry != null ; entry = entry.next) { if ((entry.hash == hash) && entry.key.equals(key)) { V old = entry.value; entry.value = value; // 返回舊值 return old; } } // 新增新的Entry addEntry(hash, key, value, index); return null; } // 新增新的entry private void addEntry(int hash, K key, V value, int index) { modCount++; Entry<?,?> tab[] = table; // 若當前hash表的Entry數量超過閾值,則重新計算hash值,將Entry重新分配位置,並當前key value需要在陣列中新增的索引位置 if (count >= threshold) { // Rehash the table if the threshold is exceeded rehash(); tab = table; hash = key.hashCode(); index = (hash & 0x7FFFFFFF) % tab.length; } // Creates the new entry. @SuppressWarnings("unchecked") // 新增Entry Entry<K,V> e = (Entry<K,V>) tab[index]; tab[index] = new Entry<>(hash, key, value, e); count++; } // 重新計算hash值,重新分配所有Entry的位置 protected void rehash() { int oldCapacity = table.length; Entry<?,?>[] oldMap = table; // overflow-conscious code // 新的容量為舊的容量2倍+1 int newCapacity = (oldCapacity << 1) + 1; if (newCapacity - MAX_ARRAY_SIZE > 0) { if (oldCapacity == MAX_ARRAY_SIZE) // Keep running with MAX_ARRAY_SIZE buckets return; newCapacity = MAX_ARRAY_SIZE; } // 新建新容量的Entry陣列 Entry<?,?>[] newMap = new Entry<?,?>[newCapacity]; modCount++; threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1); table = newMap; // 遍歷舊陣列中所有的Entry,重新放置到新的陣列中 for (int i = oldCapacity ; i-- > 0 ;) { for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) { Entry<K,V> e = old; old = old.next; int index = (e.hash & 0x7FFFFFFF) % newCapacity; e.next = (Entry<K,V>)newMap[index]; newMap[index] = e; } } }
remove
// 移除 public synchronized V remove(Object key) { Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; @SuppressWarnings("unchecked") Entry<K,V> e = (Entry<K,V>)tab[index]; // 查詢key的對映,刪除並更新連結串列 for(Entry<K,V> prev = null ; e != null ; prev = e, e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { modCount++; if (prev != null) { prev.next = e.next; } else { // 節點是首節點 tab[index] = e.next; } count--; V oldValue = e.value; e.value = null; return oldValue; } } return null; }
get
// 獲取value public synchronized V get(Object key) { Entry<?,?> tab[] = table; int hash = key.hashCode(); // 計算key在陣列中的索引位置 int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) { // 如果key的hash值相同,且key equals,則說明找到相應的對映 if ((e.hash == hash) && e.key.equals(key)) { return (V)e.value; } } return null; }
contains
// 查詢是否包含value
public synchronized boolean contains(Object value) {
if (value == null) {
throw new NullPointerException();
}
Entry<?,?> tab[] = table;
for (int i = tab.length ; i-- > 0 ;) {
for (Entry<?,?> e = tab[i] ; e != null ; e = e.next) {
if (e.value.equals(value)) {
return true;
}
}
}
return false;
}
// 查詢是否包含key
public synchronized boolean containsKey(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
return true;
}
}
return false;
}