HashTable

本文主要記錄閱讀HashTable源碼的過程。java

一、類圖結構算法

二、內部子類code

子類 做用
KeySet 集合,存放HashTable中全部的key
Entry HashTable的元素,以鏈表的形式存放在HashTable的每個index處
EntrySet 集合,存放全部的Entry節點
Enumerator  
ValueCollection  

 二、初始化blog

Hashtable的默認大小是11,平衡因子是0.75ci

三、主要操做rem

須要注意的是,HashTable類中的方法前面都有synchronized關鍵字,代表HashTable是可同步的同步

put:添加元素源碼

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();//計算hash值
        int index = (hash & 0x7FFFFFFF) % tab.length;//計算對應的index
        @SuppressWarnings("unchecked")
        Entry<K,V> entry = (Entry<K,V>)tab[index];
        for(; entry != null ; entry = entry.next) {//變量該index對應的「桶」是否已經存在相同的key,若是存在則用value替換原來的value
            if ((entry.hash == hash) && entry.key.equals(key)) {
                V old = entry.value;
                entry.value = value;
                return old;
            }
        }

        addEntry(hash, key, value, index);//向table中添加元素
        return null;
    }
	
private void addEntry(int hash, K key, V value, int index) {
	modCount++;//結構修改次數+1

	Entry<?,?> tab[] = table;
	if (count >= threshold) {//若是超過閾值,則從新擴容
		// Rehash the table if the threshold is exceeded
		rehash();

		tab = table;
		hash = key.hashCode();//擴容後從新計算index
		index = (hash & 0x7FFFFFFF) % tab.length;
	}

	// Creates the new entry.
	@SuppressWarnings("unchecked")
	Entry<K,V> e = (Entry<K,V>) tab[index];
	tab[index] = new Entry<>(hash, key, value, e);
	count++;
}
//擴容
protected void rehash() {
	int oldCapacity = table.length;
	Entry<?,?>[] oldMap = table;

	// overflow-conscious code
	int newCapacity = (oldCapacity << 1) + 1; //capacity翻倍
	if (newCapacity - MAX_ARRAY_SIZE > 0) {
		if (oldCapacity == MAX_ARRAY_SIZE)
			// Keep running with MAX_ARRAY_SIZE buckets
			return;
		newCapacity = MAX_ARRAY_SIZE;
	}
	Entry<?,?>[] newMap = new Entry<?,?>[newCapacity];

	modCount++;
	threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
	table = newMap;

	for (int i = oldCapacity ; i-- > 0 ;) {//變量原有table中每個桶
		for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) {//遍歷桶中每個元素,從新經過hash值計算index
			Entry<K,V> e = old;
			old = old.next;

			int index = (e.hash & 0x7FFFFFFF) % newCapacity;
			e.next = (Entry<K,V>)newMap[index];
			newMap[index] = e;
		}
	}
}

 

removehash

//刪除元素
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];//獲取桶中鏈表頭
	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;
}
相關文章
相關標籤/搜索