HashTabel
*** 结构**
Hashtable 的函数都是同步的,这意味着它是线程安全的。它的key、value都不可以为null。 Hashtable中的映射不是有序的。
//存储元素的节点数组
private transient Entry<?,?>[] table;
//总量
private transient int count;
//当表的大小超过此阈值时,将对其进行重新哈希处理。(此字段的值为(int)(容量* loadFactor))
private int threshold;
//负载因子
private float loadFactor;
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;
this.loadFactor = loadFactor;
table = new Entry<?,?>[initialCapacity];
threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
}
public Hashtable(int initialCapacity) {
this(initialCapacity, 0.75f);
}
public Hashtable() {
this(11, 0.75f);
}
public Hashtable(Map<? extends K, ? extends V> t) {
this(Math.max(2*t.size(), 11), 0.75f);
putAll(t);
}
*** 新增**
put(K key, V value)
//synchronized修饰,线程安全的,但是会牺牲性能
public synchronized V put(K key, V value) {
//value不能为null
// 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;
//如果key为null,会报空指针异常
int hash = key.hashCode();
//0x7FFFFFFF是一个用16进制表示的整型,是整型里面的最大值
//hash & 0x7FFFFFFF得到一个正数 再对数组长度取模,得到该hash值对应的下标
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index];
for(; entry != null ; entry = entry.next) {
//如果匹配到了,直接覆盖value
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;
//如果count(容量)大于等于临界值
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];
//在该Index位置下,新建Entry
tab[index] = new Entry<>(hash, key, value, e);
count++;
}
//HashTabel的扩容
protected void rehash() {
int oldCapacity = table.length;
Entry<?,?>[] oldMap = table;
// overflow-conscious code
//新容量=2*旧容量+1
int newCapacity = (oldCapacity << 1) + 1;
//如果新容量比最大值(Integer.MaxValue-8)还大
if (newCapacity - MAX_ARRAY_SIZE > 0) {
//如果旧容量等于该最大值,直接返回
if (oldCapacity == MAX_ARRAY_SIZE)
// Keep running with MAX_ARRAY_SIZE buckets
return;
//则让新容量为最大值
newCapacity = MAX_ARRAY_SIZE;
}
//根据新容量建tabel
Entry<?,?>[] newMap = new Entry<?,?>[newCapacity];
modCount++;
//计算新的临界值,新容量*负载因子和Integer的最大值-7中小的一个
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 ; ) {
//当前节点赋给e
Entry<K,V> e = old;
//便于循环
old = old.next;
//重新计算每一个hash值对应的索引下标
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
//赋值,将newMap[index]赋给e.next,把之前的值赋给e的后继节点
e.next = (Entry<K,V>)newMap[index];
//将e赋给index下标位置
newMap[index] = e;
}
}
}
*** 查询**
get(Object key)
public synchronized V get(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode();
//找到该hash值对应的下标
int index = (hash & 0x7FFFFFFF) % tab.length;
//循环,匹配到相同key便返回
for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
return (V)e.value;
}
}
//没有匹配到就返回null
return null;
}
*** 删除**
remove(Object key),找到key对应hash值所在下标,然后遍历匹配,匹配到之后把next指向prev.next
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;
}