HashtableNameHashtableSynopsis
DescriptionThe Hashtable class is a concrete subclass of Dictionary that builds a table of key/value pairs. Any non-null object can be used as a key or as a value. The objects used as keys must implement the equals() and hashCode() methods in a way that computes comparisons and hashcodes from the contents of an object. Once the table is built, a value can be efficiently retrieved by supplying its associated key. Hashtable is an excellent example of how a well-written class can hide an arcane algorithm. The casual user simply instantiates a Hashtable and uses put() and get() to add and retrieve key and value pairs. However, when performance is an issue, you need to be aware of the considerations discussed in the following paragraphs. Internally, a Hashtable keeps an array of key/value pairs. When a new key/value pair is added to a Hashtable, it is added to the array at an index that is calculated from the hashcode of the key. If a key/value pair already exists at this index, the new pair is linked to the existing key and value. Thus, a Hashtable has an overall structure of an array of linked lists. For a given key, the retrieval of the matching value from a Hashtable is quite fast. The Hashtable computes the hashcode of the key and uses it as an index into the array. Then it only needs to search the linked list of key/value pairs at that index to find a match for the given key. If the array is short, but the Hashtable contains many key/value pairs, however, the linked lists will be lengthy, which adversely affects performance. A Hashtable has a capacity, which is the length of its array, and a load factor, which determines when rehashing is performed. The load factor is a number between 0 and 1. If the number of key/value pairs added to the Hashtable exceeds the capacity multiplied by the load factor, the capacity of the Hashtable is increased and the key/value pairs are rehashed into the new array. Obviously, this is an undesirable performance hit, so if you know approximately how many items you will add to a Hashtable, you should create one with an appropriate initial capacity. Class Summary
public class java.util.Hashtable extends java.util.Dictionary implements java.lang.Cloneable, java.io.Serializable { // Constructors public Hashtable(); public Hashtable(int initialCapacity); public Hashtable(int initialCapacity, float loadFactor); // Instance Methods public synchronized void clear(); public synchronized Object clone(); public synchronized boolean contains(Object value); public synchronized boolean containsKey(Object key); public synchronized Enumeration elements(); public synchronized Object get(Object key); public boolean isEmpty(); public synchronized Enumeration keys(); public synchronized Object put(Object key, Object value); public synchronized Object remove(Object key); public int size(); public synchronized String toString(); // Protected Instance Methods protected void rehash(); } ConstructorsHashtablepublic Hashtable()
public Hashtable(int initialCapacity)
public Hashtable(int initialCapacity, float loadFactor)
Instance Methodsclearpublic synchronized void clear()
clonepublic synchronized Object clone()
containspublic synchronized boolean contains(Object value)
containsKeypublic synchronized boolean containsKey(Object key)
elementspublic synchronized Enumeration elements()
getpublic synchronized Object get(Object key)
isEmptypublic boolean isEmpty()
keyspublic synchronized Enumeration keys()
putpublic synchronized Object put(Object key, Object value)
removepublic synchronized Object remove(Object key)
sizepublic int size()
toStringpublic String toString()
Protected Instance Methodsrehashprotected void rehash()
Inherited Methods
See AlsoCloneable, Dictionary, Enumeration, IllegalArgumentException, NullPointerException, Properties, Serializable | ||||||||||||||||||||||||
|