Package java.util |
![]() Previous |
![]() Java API |
![]() Index |
![]() Next |
public class java.util.Hashtable extends java.util.Dictionary implements java.lang.Cloneable { // Constructors public Hashtable(); public Hashtable(int initialCapacity); public Hashtable(int initialCapacity, float loadFactor); // Methods public void clear(); public Object clone(); public boolean contains(Object value); public boolean containsKey(Object key); public Enumeration elements(); public Object get(Object key); public boolean isEmpty(); public Enumeration keys(); public Object put(Object key, Object value); protected void rehash(); public Object remove(Object key); public int size(); public String toString(); }
This class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value.
To successfully store and retrieve objects from a hash table, the objects used as keys must implement the hashCode method and the equals method .
An instance of 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 hash table exceeds the product of the load factor and the current capacity, the capacity is increased by calling 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 into 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.
This example creates a hashtable of numbers. It uses the names of the numbers as keys:
Hashtable numbers = new Hashtable(); numbers.put("one", new Integer(1)); numbers.put("two", new Integer(2)); numbers.put("three", new Integer(3));
To retrieve a number use the following code:
Integer n = (Integer)numbers.get("two"); if (n != null) { System.out.println("two = " + n); }
public Hashtable()Constructs a new empty hashtable.
public Hashtable(int initialCapacity)Constructs a new, empty hash table with the specified initial capacity.
Parameter Description initialCapacity the initial capacity of the hash table
public Hashtable(int initialCapacity, float loadFactor)Constructs a new, empty hashtable with the specified initial capacity and the specified load factor.
Parameter Description initialCapacity the initial size of the hashtable loadFactor a number between 0.0 and 1.0, Throw:
If the initial capacity is less than or equal to zero, or if the load factor is less than or equal to zero.
public void clear()Clears this hash table so that it contains no keys.
public Object clone()Creates a shallow copy of this hash table. The keys and values themselves are not cloned.
Return Value:
Returns a clone of the hash table.
Overrides:
clone in class Object .
public boolean contains(Object value)Return Value:
Returns true if some key maps to the value argument in this hash table; false otherwise.
Parameter Description value a value to search for Throw:
If the value is null.
See Also: containsKey .
public boolean containsKey(Object key)Return Value:
Returns true if the specified object is a key in this hash table; false otherwise.
Parameter Description key possible key See Also: contains .
public Enumeration elements()Return Value:
Returns an enumeration of the values in this hash table.
Overrides:
elements in class Dictionary .
See Also: keys .
public Object get(Object key)Return Value:
Returns the value to which the key is mapped in this hash table; null if the key is not mapped to any value is this hash table.
Parameter Description key a key in the hash table Overrides:
get in class Dictionary .
See Also: put .
public boolean isEmpty()Return Value:
Returns true if this hash table maps no keys to values; false otherwise.
Overrides:
isEmpty in class Dictionary .
public Enumeration keys()Return Value:
Returns an enumeration of the keys in this hash table.
Overrides:
keys in class Dictionary .
See Also: elements .
public Object put(Object key, Object value)Maps the specified key to the specified value in this hash table. Neither the key nor the value can be null.
The value can be retrieved by calling the the get method with a key that is equal to the original key.
Return Value:
Returns the previous value of of the specified key in this hash table, or null if it did not have one.
Parameter Description key the hashtable key value the value Throw:
If the key or value is null.
Overrides:
put in class Dictionary .
protected void rehash()Rehashes the contents of the hash table into a hash table with a larger capacity. This method is called automatically when the number of keys in the hash table exceeds this hash table's capacity and load factor.
public Object remove(Object key)Removes the key (and its corresponding value) from this hash table. This method does nothing if the key is not in the hash table.
Return Value:
Returns the value to which the key had been mapped in this hash table, or null if the key did not have a mapping.
Parameter Description key the key that needs to be removed Overrides:
remove in class Dictionary .
public int size()Return Value:
Returns the number of keys in this hash table.
Overrides:
size in class Dictionary .
public String toString()Return Value:
Returns a string representation of this hash table.
Overrides:
toString in class Object .