java.util
Class Hashtable

java.lang.Object
  |
  +--java.util.Dictionary
        |
        +--java.util.Hashtable
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
Properties

public class Hashtable
extends Dictionary
implements Cloneable

Hashtable class. Maps keys to values. Any object can be used as a key and/or value.

To sucessfully store and retrieve objects from a hash table, the object used as the key must implement the hashCode() and equals() methods.

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:
	Integer n = (Integer)numbers.get("two");
	if (n != null) {
	    System.out.println("two = " + n);
	}
 

See Also:
Object.hashCode(), Object.equals(java.lang.Object)

Constructor Summary
Hashtable()
          Constructs a new, empty hashtable.
Hashtable(int initialCapacity)
          Constructs a new, empty hashtable with the specified initial capacity.
Hashtable(int initialCapacity, float loadFactor)
          Constructs a new, empty hashtable with the specified initial capacity and the specified load factor.
 
Method Summary
 void clear()
          Clears the hash table so that it has no more elements in it.
 Object clone()
          Creates a clone of the hashtable.
 boolean contains(Object value)
          Returns true if the specified object is an element of the hashtable.
 boolean containsKey(Object key)
          Returns true if the collection contains an element for the key.
 Enumeration elements()
          Returns an enumeration of the elements.
 Object get(Object key)
          Gets the object associated with the specified key in the hashtable.
 boolean isEmpty()
          Returns true if the hashtable contains no elements.
 Enumeration keys()
          Returns an enumeration of the hashtable's keys.
 Object put(Object key, Object value)
          Puts the specified element into the hashtable, using the specified key.
protected  void rehash()
          Rehashes the content of the table into a bigger table.
 Object remove(Object key)
          Removes the element corresponding to the key.
 int size()
          Returns the number of elements contained in the hashtable.
 String toString()
          Converts to a rather lengthy String.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Hashtable

public Hashtable(int initialCapacity,
                 float loadFactor)
Constructs a new, empty hashtable with the specified initial capacity and the specified load factor.

Parameters:
initialCapacity - the initial number of buckets
loadFactor - a number between 0.0 and 1.0, it defines the threshold for rehashing the hashtable into a bigger one.
Throws:
IllegalArgumentException - If the initial capacity is less than or equal to zero.
IllegalArgumentException - If the load factor is less than or equal to zero.

Hashtable

public Hashtable(int initialCapacity)
Constructs a new, empty hashtable with the specified initial capacity.

Parameters:
initialCapacity - the initial number of buckets

Hashtable

public Hashtable()
Constructs a new, empty hashtable. A default capacity and load factor is used. Note that the hashtable will automatically grow when it gets full.

Method Detail

size

public int size()
Returns the number of elements contained in the hashtable.

Specified by:
size in class Dictionary

isEmpty

public boolean isEmpty()
Returns true if the hashtable contains no elements.

Specified by:
isEmpty in class Dictionary

keys

public Enumeration keys()
Returns an enumeration of the hashtable's keys.

Specified by:
keys in class Dictionary
See Also:
elements(), Enumeration

elements

public Enumeration elements()
Returns an enumeration of the elements. Use the Enumeration methods on the returned object to fetch the elements sequentially.

Specified by:
elements in class Dictionary
See Also:
keys(), Enumeration

contains

public boolean contains(Object value)
Returns true if the specified object is an element of the hashtable. This operation is more expensive than the containsKey() method.

Parameters:
value - the value that we are looking for
Throws:
NullPointerException - If the value being searched for is equal to null.
See Also:
containsKey(java.lang.Object)

containsKey

public boolean containsKey(Object key)
Returns true if the collection contains an element for the key.

Parameters:
key - the key that we are looking for
See Also:
contains(java.lang.Object)

get

public Object get(Object key)
Gets the object associated with the specified key in the hashtable.

Specified by:
get in class Dictionary
Parameters:
key - the specified key
See Also:
put(java.lang.Object, java.lang.Object)

rehash

protected void rehash()
Rehashes the content of the table into a bigger table. This method is called automatically when the hashtable's size exceeds the threshold.


put

public Object put(Object key,
                  Object value)
Puts the specified element into the hashtable, using the specified key. The element may be retrieved by doing a get() with the same key. The key and the element cannot be null.

Specified by:
put in class Dictionary
Parameters:
key - the specified key in the hashtable
value - the specified element
Returns:
the old value of the key, or null if it did not have one.
Throws:
NullPointerException - If the value of the element is equal to null.
See Also:
get(java.lang.Object)

remove

public Object remove(Object key)
Removes the element corresponding to the key. Does nothing if the key is not present.

Specified by:
remove in class Dictionary
Parameters:
key - the key that needs to be removed
Returns:
the value of key, or null if the key was not found.

clear

public void clear()
Clears the hash table so that it has no more elements in it.


clone

public Object clone()
Creates a clone of the hashtable. A shallow copy is made, the keys and elements themselves are NOT cloned. This is a relatively expensive operation.

Overrides:
clone in class Object
Returns:
a clone of this Object.

toString

public String toString()
Converts to a rather lengthy String.

Overrides:
toString in class Object