一、构造⽅法

Hashtable⼀共提供了4个构造⽅法:
public Hashtable(int initialCapacity, float loadFactor): ⽤指定初始容量和指定加载因⼦构
造⼀个新的空哈希表。useAltHashing为boolean,其如果为真,则执⾏另⼀散列的字符串
键,以减少由于弱哈希计算导致的哈希冲突的发⽣。
public Hashtable(int initialCapacity):⽤指定初始容量和默认的加载因⼦ (0.75) 构造⼀个新
的空哈希表。
public Hashtable():默认构造函数,容量为11,加载因⼦为0.75。
public Hashtable(Map<? extends K, ? extends V> t):构造⼀个与给定的 Map 具有相同映
射关系的新哈希表。
/**
* Constructs a new, empty hashtable with the specified initial
* capacity and the specified load factor.
*
* @param initialCapacity the initial capacity of the hashtable.
* @param loadFactor the load factor of the hashtable.
* @exception IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive.
/
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) ;
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).
*
* @param initialCapacity the initial capacity of the hashtable.
* @exception IllegalArgumentException if the initial capacity is less
* than zero.
/
public Hashtable(int initialCapacity) {
this(initialCapacity, 0.75f);
}
/*

* Constructs a new, empty hashtable with a default initial capacity (11)
* and load factor (0.75).
/
public Hashtable() {
this(11, 0.75f);
}
/*

* 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).
*
* @param t the map whose mappings are to be placed in this map.
* @throws NullPointerException if the specified map is null.
* @since 1.2
/
public Hashtable(Map<? extends K, ? extends V> t) {
this(Math.max(2
t.size(), 11), 0.75f);
putAll(t);
}

二、put⽅法

put⽅法的整个流程为:
判断value是否为空,为空则抛出异常;
计算key的hash值,并根据hash值获得key在table数组中的位置index,如果table[index]元素
不为空,则进⾏迭代,如果遇到相同的key,则直接替换,并返回旧value;
否则,我们可以将其插⼊到table[index]位置。
public synchronized V put(K key, V value) {
// Make sure the value is not null确保value不为null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
//确保key不在hashtable中
//⾸先,通过hash⽅法计算key的哈希值,并计算得出index值,确定其在table[]中的位置
//其次,迭代index索引位置的链表,如果该位置处的链表存在相同的key,则替换value,返
回旧的value
Entry tab[] = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;
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操作
rehash();
tab = table;
hash = hash(key);
index = (hash & 0x7FFFFFFF) % tab.length;
}
// Creates the new entry.
//将值插⼊,返回的为null
Entry<K,V> e = tab[index];
// 创建新的Entry节点,并将新的Entry插⼊Hashtable的index位置,并设置e为新的Entry
的下⼀个元素
tab[index] = new Entry<>(hash, key, value, e);
count++;
return null;
}
通过⼀个实际的例⼦来演示⼀下这个过程:
假设我们现在Hashtable的容量为5,已经存在了(5,5),(13,13),(16,16),(17,17),(21,21)这5个
键值对,⽬前他们在Hashtable中的位置如下:
图片说明
现在,我们插⼊⼀个新的键值对,put(16,22),假设key=16的索引为1.但现在索引1的位置有两个
Entry了,所以程序会对链表进⾏迭代。迭代的过程中,发现其中有⼀个Entry的key和我们要插⼊
的键值对的key相同,所以现在会做的⼯作就是将newValue=22替换oldValue=16,然后返回
oldValue=16.
图片说明
然后我们现在再插⼊⼀个,put(33,33),key=33的索引为3,并且在链表中也不存在key=33的
Entry,所以将该节点插⼊链表的第⼀个位置。
图片说明
图片说明

大家一起学习丫~