21.5 The Class java.util.Hashtable

. . . never did they seem to have new experiences in common . . . and the things they had for dissection—college, contemporary personality, and the like—they had hashed and rehashed for many a frugal conversational meal.
—F. Scott Fitzgerald, This Side of Paradise (1920)

The class Hashtable implements the abstract class Dictionary (§21.4), with some additional functionality.

public class Hashtable extends Dictionary implements Cloneable {
	public Hashtable(int initialCapacity, float loadFactor);
	public Hashtable(int initialCapacity);
	public Hashtable();
	public String toString();
	public Object clone();
	public int size();
	public boolean isEmpty();
	public Object get(Object key)
		throws NullPointerException;
	public Object put(Object key, Object value)
		throws NullPointerException;
	public Object remove(Object key)
		throws NullPointerException;
	public Enumeration keys();
	public Enumeration elements();
	public boolean contains(Object value);
	public boolean containsKey(Object key);
	protected void rehash();
	public void clear();
}

A Hashtable has two parameters that affect its efficiency: its capacity and its load factor. The load factor should be between 0.0 and 1.0. When the number of entries in the hashtable exceeds the product of the load factor and the current capacity, the capacity is increased, using the rehash method. Larger load factors use memory more efficiently at the expense of larger expected time per lookup. If many entries are to be made in a Hashtable, creating it with a sufficiently large capacity may allow the entries to be inserted more efficiently than letting it perform automatic rehashing as needed to grow the table.

21.5.1 public Hashtable(int initialCapacity, float loadFactor)

This constructor initializes a newly created Hashtable object so that its capacity is initialCapacity and its load factor is loadFactor. Initially, there are no entries in the table.

21.5.2 public Hashtable(int initialCapacity)

This constructor initializes a newly created Hashtable object so that its capacity is initialCapacity and its load factor is 0.75. Initially, there are no entries in the table.

21.5.3 public Hashtable()

This constructor initializes a newly created Hashtable object so that its load factor is 0.75. Initially, there are no entries in the table.

21.5.4 public String toString()

This Hashtable is represented in string form as a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space). Each entry is rendered as the key, an equals sign =, and the associated element, where the toString method is used to convert the key and element to strings.

Overrides the toString method of Object (§21.2.3).

21.5.5 public Object clone()

A copy of this Hashtable is constructed and returned. All the structure of the hashtable itself is copied, but the keys and elements are not cloned.

Overrides the clone method of Object (§21.2.6).

21.5.6 public int size()

Implements the size method of Dictionary (§21.4.1).

21.5.7 public boolean isEmpty()

Implements the isEmpty method of Dictionary (§21.4.2).

21.5.8 public Object get(Object key)

Implements the get method of Dictionary (§21.4.3).

21.5.9 public Object put(Object key, Object value)

Implements the put method of Dictionary (§21.4.4).

21.5.10 public Object remove(Object key)

Implements the remove method of Dictionary (§21.4.5).

21.5.11 public Enumeration keys()

Implements the keys method of Dictionary (§21.4.6).

21.5.12 public Enumeration elements()

Implements the elements method of Dictionary (§21.4.7).

21.5.13 public boolean contains(Object value)

The result is true if and only if this Hashtable contains at least one entry for which the element is equal to value, as determined by the equals method (§20.1.3).

21.5.14 public boolean containsKey(Object key)

The result is true if and only if this Hashtable contains an entry for which the key is equal to key, as determined by the equals method (§20.1.3). In other words, this method produces the same result as the expression:

get(key) != null

21.5.15 protected void rehash()

This Hashtable is increased in capacity and reorganized internally, in order to accommodate and access its entries more efficiently.

21.5.16 public void clear()

The clear method removes all entries from this Hashtable.

Twelve sphered tables, by silk seats insphered,
High as the level of a man's breast rear'd
On libbard's paws, upheld the heavy gold
Of cups and goblets, and the store thrice told
Of Ceres' horn, and, in huge vessels, wine
Came from the gloomy tun with merry shine.
Thus loaded with a feast the tables stood . . .
—John Keats, Lamia Part II