Hashtable产生于JDK 1.1,而HashMap产生于JDK 1.2,Hashtable基本已经弃用了,虽然Hashtable是线程安全的,但是并发大神Doug Lea写了util.concurrent包,高并发情况下可以使用并发包里的ConcurrentHashMap。如果向Hashtable里添加的节点key或者value为nil会触发java.lang.NullPointerException。
建议先看Java集合源码分析-HashMap,然后再看Hashtable就很简单了。
Hashtable底层的数据结构和HashMap一样,都是一个hash桶,每个桶位存储的都是一个单向非循环的链表。
Hashtable类图
可以看到Hashtable的父类是Dictionary。
Hashtable成员变量和构造器
private transient Hashtable.Entry<?, ?>[] table;
private transient int count;
private int threshold;
private float loadFactor;
private transient int modCount;
private static final long serialVersionUID = 1421746759512286392L;
private static final int MAX_ARRAY_SIZE = 2147483639;
private transient volatile Set<K> keySet;
private transient volatile Set<java.util.Map.Entry<K, V>> entrySet;
private transient volatile Collection<V> values;
private static final int KEYS = 0;
private static final int VALUES = 1;
private static final int ENTRIES = 2;
和HashMap一样,Hashtable提供了四类构造器:
public Hashtable() {
this(11, 0.75f);
}
public Hashtable(int initialCapacity) {
this(initialCapacity, 0.75f);
}
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;
table = new Entry<?,?>[initialCapacity];
threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
}
public Hashtable(Map<? extends K, ? extends V> t) {
this(Math.max(2*t.size(), 11), 0.75f);
putAll(t);
}
可以看到和HashMap的构造器所做的操作基本是一样的而且比HashMap还要简单,因为不要求hash桶的容量必须是2的次幂。
然后分析下Hashtable的核心操作:put、remove
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;
}
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++;
}
protected void rehash() {
int oldCapacity = table.length;
Entry<?,?>[] oldMap = table;
// overflow-conscious code
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;
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;
}
}
}
需要注意方法是synchronized的就行了,然后Hashtable是将新节点插入到了链表的头部,而HashMap是将新节点插入到了链表的尾部,其实插入头部还是尾部是无所谓的,不存在孰优孰劣(因为两个插入方式都要遍历链表,遇到相同key值则break并覆盖,没有相同key就遍历到尾部,所以两种方式性能是一样的)。
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];
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;
}