//底层实现:哈希表+链表privatetransient Entry<?,?>[] table;//存储数组privatetransientint count;//容器中数据多少privateint threshold;//容器容量达到次数以后进行修改privatetransientint modCount =0;//修改次数//Hash函数 int hash = key.hashCode();//在这两行 int index =(hash &0x7FFFFFFF)% tab.length;//在这两行 //初始化。在构造方法中初始化。初始化指为11publicHashtable(int initialCapacity,float loadFactor){if(initialCapacity <0)thrownewIllegalArgumentException("Illegal Capacity: "+
initialCapacity);if(loadFactor <=0|| Float.isNaN(loadFactor))thrownewIllegalArgumentException("Illegal Load: "+loadFactor);if(initialCapacity==0)
initialCapacity =1;this.loadFactor = loadFactor;
table =newEntry<?,?>[initialCapacity];
threshold =(int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE +1);}//put方法publicsynchronized V put(K key, V value){// Make sure the value is not nullif(value == null){thrownewNullPointerException();}// Makes sure the key is not already in the hashtable.
Entry<?,?> tab[]= table;//hash函数计算一个indexint 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;}//增加实体privatevoidaddEntry(int hash, K key, V value,int index){
Entry<?,?> tab[]= table;if(count >= threshold){// Rehash the table if the threshold is exceededrehash();
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]=newEntry<>(hash, key, value, e);
count++;
modCount++;}