Java

HashTable

⼀、概述
和HashMap⼀样,Hashtable也是⼀个散列表,它存储的内容是键值对。
Hashtable在Java中的定义为:
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, java.io.Serializable{}
从源码中,我们可以看出,Hashtable继承于Dictionary类,实现了Map, Cloneable,
java.io.Serializable接⼝。其中Dictionary类是任何可将键映射到相应值的类(如 Hashtable)的
抽象⽗类,每个键和值都是对象(源码注释为:The Dictionary class is the abstract parent of
any class, such as Hashtable, which maps keys to values. Every key and every value is an
object.)。但其Dictionary源码注释是这样的:NOTE: This class is obsolete. New
implementations should implement the Map interface, rather than extending this class. 该话指
出Dictionary这个类过时了,新的实现类应该实现Map接⼝。

⼆、成员变量

Hashtable是通过”拉链法”实现的哈希表。它包括⼏个重要的成员变量:table, count, threshold,
loadFactor, modCount。
table是⼀个Entry[]数组类型,⽽Entry(在HashMap中有讲解过)实际上就是⼀个单向链
表。哈希表的”key-value键值对”都是存储在Entry数组中的。
count是Hashtable的⼤⼩,它是Hashtable保存的键值对的数量。
threshold是Hashtable的阈值,⽤于判断是否需要调整Hashtable的容量。threshold的值
=”容量加载因⼦”。
loadFactor就是加载因⼦。
modCount是⽤来实现fail-fast机制的。
变量的解释在源码注释中如下:
/*

* The hash table data.
/
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).)
*
* @serial
/
private int threshold;
/*

* The load factor for the hashtable.
*
* @serial
/
private float loadFactor;
/*

* The number of times this Hashtable has been structurally modified
* Structural modifications are those that change the number of entries in
* the Hashtable or otherwise modify its internal structure (e.g.,
* rehash). This field is used to make iterators on Collection-views of
* the Hashtable fail-fast. (See ConcurrentModificationException).
*/
private transient int modCount = 0;

大家一起学习丫~