java.util.Hashtable
java.util.Dictionary
java.util.Properties
java.lang.Cloneable, java.io.Serializable
JDK 1.0 or later
The 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.
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(); }
This constructor creates a Hashtable with a default capacity of 101 and a default load factor of .75.
The initial capacity.
If initialCapacity is less than or equal to zero.
This constructor creates a Hashtable with the given capacity and a default load factor of .75.
The initial capacity.
The load factor.
If initialCapacity or loadFactor is less than or equal to zero.
This constructor creates a Hashtable with the given capacity and load factor.
This method removes all of the key/value pairs from this Hashtable.
A copy of this Hashtable.
Object.clone()
This method returns a shallow copy of this Hashtable. This means that the internal array of the Hashtable is copied, but the keys and values themselves are not copied.
The value to find.
true if this Hashtable contains the given value; false otherwise.
If the given value is null.
This method returns true if the given value is contained in this Hashtable object. The entire table is searched, which can be a time-consuming operation.
The key to find.
true if this Hashtable contains the given value; false otherwise.
This method returns true if the given key is contained in this Hashtable object. Because the key is hashed to perform the search, this method runs quite fast, especially in comparison to contains().
The values in this Hashtable as an Enumeration.
Dictionary.elements()
This method returns an Enumeration that iterates through the values in this Hashtable.
The key of the value to retrieve.
The value that corresponds to this key or null if the key is not associated with any value.
Dictionary.get()
This method returns the value that is associated with the given key.
true if there are no values in the Hashtable; false otherwise.
Dictionary.isEmpty()
This method returns a boolean value that indicates whether or not the Hashtable is empty.
The keys in the Hashtable as an Enumeration.
Dictionary.keys()
This method returns an Enumeration that iterates through the keys in this Hashtable.
A key object.
A value object.
The previous value associated with the given key or null if key has not previously been associated with a value.
If either the key or the value is null.
Dictionary.put()
This method associates the given key with the given value in this Hashtable.
A key of the value to remove.
The value associated with the given key, or null if key is not associated with a value.
Dictionary.remove()
This method removes a key/value pair from this Hashtable. If the given key is not in the Hashtable, the method does nothing.
The number of key in the Hashtable.
Dictionary.size()
This method returns the number of key/value pairs in the Hashtable.
A string that represents this Hashtable.
Object.toString()
This method returns a string representation of this Hashtable. The string includes every key/value pair that is contained in the Hashtable, so the string returned by toString() can be quite long.
This method increases the capacity of this Hashtable. A larger internal array is created and all existing key/value pairs are rehashed into the new array.
Method |
Inherited From |
Method |
Inherited From |
---|---|---|---|
equals(Object) |
Object |
finalize() |
Object |
getClass() |
Object |
hashCode() |
Object |
notify() |
Object |
notifyAll() |
Object |
wait() |
Object |
wait(long) |
Object |
wait(long, int) |
Object |
Cloneable, Dictionary, Enumeration, IllegalArgumentException, NullPointerException, Properties, Serializable
This HTML Help has been published using the chm2web software. |