HashTable经常拿来跟HashMap比较,今天来看看HashTable的源码。从无参构造方法出发。
//初始的容量为11,装载因子为0.75。跟HashMap一样。初始化时平平无奇。
public Hashtable(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal Load: "+loadFactor);
if (initialCapacity==0)
initialCapacity = 1;
this.loadFactor = loadFactor;
//HashTableEntry属于单向链表。
table = new HashtableEntry<?,?>[initialCapacity];
// Android-changed: Ignore loadFactor when calculating threshold from initialCapacity
// threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
// threshold 为阀值,代表超过这个值就需要扩容。
threshold = (int)Math.min(initialCapacity, MAX_ARRAY_SIZE + 1);
}
现在我们开始分析主要逻辑,put和get。同步方法。
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.
HashtableEntry<?,?> tab[] = table;
//直接求出key的hashcode。
int hash = key.hashCode();
//为啥要把hash值和0x7FFFFFFF做一次按位与操作呢,
// 主要是为了保证得到的index的第一位为0,也就是为了得到一个正数。
// 因为有符号数第一位0代表正数,1代表负数。
//这个方法体现了hash思想,将一个key跟一个小于tab.length进行了关联。
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
HashtableEntry<K,V> entry = (HashtableEntry<K,V>)tab[index];
//如果已经有值了,重新赋值。
for(; entry != null ; entry = entry.next) {
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value;
return old;
}
}
//如果entry为null,那么直接加入了。
addEntry(hash, key, value, index);
return null;
}
分析下addEntry 方法。
private void addEntry(int hash, K key, V value, int index) {
modCount++;
HashtableEntry<?,?> tab[] = table;
//如果达到阀值了。那么重新扩容,一会来分析。
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")
HashtableEntry<K,V> e = (HashtableEntry<K,V>) tab[index];
tab[index] = new HashtableEntry<>(hash, key, value, e);
count++;
}
protected void rehash() {
int oldCapacity = table.length;
HashtableEntry<?,?>[] 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;
}
HashtableEntry<?,?>[] newMap = new HashtableEntry<?,?>[newCapacity];
modCount++;
//重新计算阀值。
threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
table = newMap;
//老的table的赋值到新的table。
for (int i = oldCapacity ; i-- > 0 ;) {
for (HashtableEntry<K,V> old = (HashtableEntry<K,V>)oldMap[i] ; old != null ; ) {
HashtableEntry<K,V> e = old;
old = old.next;
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
e.next = (HashtableEntry<K,V>)newMap[index];
newMap[index] = e;
}
}
}
可以看到put方法中如果index碰撞了,解决思想就是直接覆盖了。但是HashMap不会,它会把对象插入对对应数组的头结点。还有一个扩容大小不一样。相比较而言,HashMap要复杂的很多了。