Hashtable is a data structure used to implement an associative array, a structure that can map keys to values. It can provide a very efficient way to search for items in large amounts of data, particularly data that is not otherwise easily searchable. Hashtable can contains only unique elements and may not have any null key or value. It is almost similar to HashMap, but is synchronized. Dictionary is an abstract base class of Hashtable. However, Java 2 re-engineered Hashtable so that it also implements the Map interface and making it a member of the Java Collections Framework.
How Hashtable works?
The hash algorithm is used to generate an index into that array based on the values of the item that will be stored in the array. The maximum size of this array is typically smaller than the number of items in the set of possible values for the type of data being stored in the hashtable. When an element is added to the Hashtable , the element is placed into a bucket based on the hash code of the key. Each bucket can have multiple records which are organized in a particular order. Subsequent lookups of the key use the hash code of the key to search in only one particular bucket, thus substantially reducing the number of key comparisons required to find an element. The load factor of a Hashtable determines the maximum ratio of elements to buckets. Smaller load factors cause faster average lookup times at the cost of increased memory consumption. An instance of Hashtable has two parameters that affect its performance: initial capacity and load factor . The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created.
The following Java program illustrates several of the methods supported by this Hashtable collection Framework