public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, java.io.Serializable
NOTE: This class is obsolete. New implementations should * implement the Map interface, rather than extending this class.
public abstract class Dictionary<K,V> { public Dictionary() { } abstract public int size(); abstract public boolean isEmpty(); abstract public Enumeration<K> keys(); abstract public Enumeration<V> elements(); abstract public V get(Object key); abstract public V put(K key, V value); abstract public V remove(Object key); }
没啥好说的,下面直接看Hashtable源码,首先依然是成员变量:
private transient Entry<K,V>[] table;//存储键值对对象的桶数组 /** * The total number of entries in the hash table. *键值对总数 */ private transient int count; /** * The table is rehashed when its size exceeds this threshold. (The * value of this field is (int)(capacity * loadFactor).) *容量的阈值,超过此容量将会导致扩容。值为容量*负载因子 */ private int threshold; /** * The load factor for the hashtable. *负载因子 */ private float loadFactor; /** * hashtable被改变的次数,用于快速失败机制 */ private transient int modCount = 0;
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;//初始容量最小值为1 this.loadFactor = loadFactor; table = new Entry[initialCapacity];//创建桶数组 threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);//初始化容量阈值 useAltHashing = sun.misc.VM.isBooted() && (initialCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD); } /** * Constructs a new, empty hashtable with the specified initial capacity * and default load factor (0.75). */ public Hashtable(int initialCapacity) { this(initialCapacity, 0.75f);//默认负载因子为0.75 } public Hashtable() { this(11, 0.75f);//默认容量为11,负载因子为0.75 } /** * Constructs a new hashtable with the same mappings as the given * Map. The hashtable is created with an initial capacity sufficient to * hold the mappings in the given Map and a default load factor (0.75). */ public Hashtable(Map<? extends K, ? extends V> t) { this(Math.max(2*t.size(), 11), 0.75f); putAll(t); }
private static class Entry<K,V> implements Map.Entry<K,V> {//键值对对象 int hash;//哈希值 final K key;//键 V value;//值 Entry<K,V> next;//指向下一个 protected Entry(int hash, K key, V value, Entry<K,V> next) { this.hash = hash; this.key = key; this.value = value; this.next = next; } protected Object clone() {//直接通过new的方式克隆 return new Entry<>(hash, key, value, (next==null ? null : (Entry<K,V>) next.clone())); } // Map.Entry Ops public K getKey() { return key; } public V getValue() { return value; } public V setValue(V value) {//可设置值 if (value == null) throw new NullPointerException(); V oldValue = this.value; this.value = value; return oldValue; } public boolean equals(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry<?,?> e = (Map.Entry)o; return key.equals(e.getKey()) && value.equals(e.getValue()); } public int hashCode() { return hash ^ value.hashCode(); } public String toString() { return key.toString()+"="+value.toString(); } }
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 = hash(key);//根据键生成hash值---->若key为null,此方法会抛异常 int index = (hash & 0x7FFFFFFF) % tab.length;//通过hash值找到其存储位置 for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {/遍历链表 if ((e.hash == hash) && e.key.equals(key)) {//若键相同,则新值覆盖旧值 V old = e.value; e.value = value; return old; } } modCount++; if (count >= threshold) {//当前容量超过阈值。需要扩容 // Rehash the table if the threshold is exceeded rehash();//重新构建桶数组,并对数组中所有键值对重哈希,耗时! tab = table; hash = hash(key); index = (hash & 0x7FFFFFFF) % tab.length;//这里是取摸运算 } // Creates the new entry. Entry<K,V> e = tab[index]; //将新结点插到链表首部 tab[index] = new Entry<>(hash, key, value, e);//生成一个新结点 count++; return null; }
private int hash(Object k) { if (useAltHashing) { if (k.getClass() == String.class) { return sun.misc.Hashing.stringHash32((String) k); } else { int h = hashSeed ^ k.hashCode(); h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); } } else { return k.hashCode();//此处可能抛空指针异常 } }
protected void rehash() { int oldCapacity = table.length;//记录旧容量 Entry<K,V>[] oldMap = table;//记录旧的桶数组 // overflow-conscious code int newCapacity = (oldCapacity << 1) + 1;//新容量为老容量的2倍加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<K,V>[] newMap = new Entry[newCapacity];//创建新的数组 modCount++; threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1); boolean currentAltHashing = useAltHashing; useAltHashing = sun.misc.VM.isBooted() && (newCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD); boolean rehash = currentAltHashing ^ useAltHashing; table = newMap; for (int i = oldCapacity ; i-- > 0 ;) {//转移键值对到新数组 for (Entry<K,V> old = oldMap[i] ; old != null ; ) { Entry<K,V> e = old; old = old.next; if (rehash) { e.hash = hash(e.key); } int index = (e.hash & 0x7FFFFFFF) % newCapacity; e.next = newMap[index]; newMap[index] = e; } } }
public synchronized V get(Object key) {//根据键取出对应索引 Entry tab[] = table; int hash = hash(key);//先根据key计算hash值 int index = (hash & 0x7FFFFFFF) % tab.length;//再根据hash值找到索引 for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {//遍历entry链 if ((e.hash == hash) && e.key.equals(key)) {//若找到该键 return e.value;//返回对应的值 } } return null;//否则返回null }
public synchronized V remove(Object key) {//删除指定键值对 Entry tab[] = table; int hash = hash(key);//计算hash值 int index = (hash & 0x7FFFFFFF) % tab.length;//计算索引 for (Entry<K,V> e = tab[index], prev = null ; e != null ; prev = e, e = e.next) {//遍历entry链 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; }
public synchronized void clear() {//清空桶数组 Entry tab[] = table; modCount++; for (int index = tab.length; --index >= 0; ) tab[index] = null;//直接置空 count = 0; }
public Set<K> keySet() { if (keySet == null)//通过Collections的包装,返回的是线程安全的键集 keySet = Collections.synchronizedSet(new KeySet(), this); return keySet; } public Set<Map.Entry<K,V>> entrySet() { if (entrySet==null)//通过Collections的包装,返回的是线程安全的键值集 entrySet = Collections.synchronizedSet(new EntrySet(), this); return entrySet; }
private class KeySet extends AbstractSet<K> { public Iterator<K> iterator() { return getIterator(KEYS); } public int size() { return count; } public boolean contains(Object o) { return containsKey(o); } public boolean remove(Object o) { return Hashtable.this.remove(o) != null; } public void clear() { Hashtable.this.clear(); } }
【源码】Hashtable源码剖析,布布扣,bubuko.com
原文地址:http://blog.csdn.net/chdjj/article/details/38581035