HashTable本身和hashMap差距不大,看了幾個hashTable的內部方法實現,發現內部方法沒有上鎖,但是用public修飾的方法全部用synchronize加上了方法鎖,這樣是極其消耗性能的,好,我們來看幾個內部方法的實現。
rehash()
protected void rehash() {
int oldCapacity = table.length;
Entry<?,?>[] oldMap = table;
// 將容量擴大為原來的兩倍+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<?,?>[] newMap = new Entry<?,?>[newCapacity];
modCount++;
threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
table = newMap;
//這個地方有點意思,跟hashMap和ConcurrentHashmap的實現不一樣
//前兩者是使用hash值與舊數組長度進行與操作,然后分成兩條鏈,直接放到相應位置
//而hashTable是從前面插入,也就是每插入一個節點,就將這個節點的next指向原有的首節點
//然后插入節點充當當前位置的首節點
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;
}
}
}
在數據遷移過程中,我們可以看到hashTable是通過取余來獲取位置的,這個效率肯定不如hashmap,還有一個地方是hash和0x7fffffff進行了與操作,這個三個集合都有點不同,hashmap是先將hash值無符號右移16位然后再跟自身進行異或操作,這個是為了減少hash沖突,但是在ConcurrentHashmap中,它的hash值計算在這個基礎上還跟0x7fffffff進行了與操作,而hashtable沒有高16和低16位的異或操作,直接跟0x7fffffff進行與操作。
總結一下:
1、hashmap跟hashtable和ConcurrentHashmap的區別在于,后兩者都是線程安全的集合,然后他們都跟0x7fffffff進行了與操作
2、hashTable的位置計算直接通過取余的方式,在位置選擇上的性能上會低于hashmap和concurrentHashmap
3、ConcurrentHashmap中有個數據遷移的地方比較高明,在分成兩條鏈進行插入的時候,它要對原有的鏈進行遍歷,在遍歷的過程中發現可以重用的節點,然后不用再二次創建這些節點,在使用中可能會大大提高性能夠,因為這個類是處理高并發的集合。
addEntry
private void addEntry(int hash, K key, V value, int index) {
modCount++;
Entry<?,?> 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")
Entry<K,V> e = (Entry<K,V>) tab[index];
//插入節點,也是一樣從鏈表頭插入
tab[index] = new Entry<>(hash, key, value, e);
count++;
}
這個方法也比較簡單,跟前面的rehash一樣,插入新節點是從鏈表頭插入的
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();
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<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;
}
}
addEntry(hash, key, value, index);
return null;
}
這個方法之所以拿出來講,我們看第一條判斷語句,value==null的判斷,也就是說hashtable不支持value為空,但是支持key為空,hashmap既支持key也支持value為空,而ConcurrentHashMap是都不支持二者為空的情況,這個也是三個集合類的區別之一。
為什么要這樣規定key或者value不能為空呢?(網上感覺比較合理的解釋如下)
ConcurrentHashmap和Hashtable都是支持并發的,這樣會有一個問題,當你通過get(k)獲取對應的value時,如果獲取到的是null時,你無法判斷,它是put(k,v)的時候value為null,還是這個key從來沒有做過映射。HashMap是非并發的,可以通過contains(key)來做這個判斷。而支持并發的Map在調用m.contains(key)和m.get(key),m可能已經不同了。
clone
//Creates a shallow copy of this hashtable. All the structure of the
// hashtable itself is copied, but the keys and values are not cloned.
//This is a relatively expensive operation
public synchronized Object clone() {
try {
Hashtable<?,?> t = (Hashtable<?,?>)super.clone();
t.table = new Entry<?,?>[table.length];
for (int i = table.length ; i-- > 0 ; ) {
t.table[i] = (table[i] != null)
? (Entry<?,?>) table[i].clone() : null;
}
t.keySet = null;
t.entrySet = null;
t.values = null;
t.modCount = 0;
return t;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError(e);
}
}
看到方法上的注釋可以知道這個類跟hashmap一樣都是淺拷貝的,key和value值不拷貝,而相比于此,ConcurrentHashMap是沒有提供拷貝方法的,也就是不能進行拷貝。