Class HashTable<T>

java.lang.Object
  extended by HashTable<T>

public class HashTable<T>
extends java.lang.Object

This class implements a hashtable that using chaining for collision handling. Any non-null item may be added to a hashtable. Chains are implemented using LinkedLists. When a hashtable is created, its initial size, maximum load factor, and (optionally) maximum chain length are specified. The hashtable can hold arbitrarily many items and resizes itself whenever it reaches its maximum load factor or whenever it reaches its maximum chain length (if a maximum chain length has been specified). Note that the hashtable allows duplicate entries.


Constructor Summary
HashTable(int initSize, double loadFactor)
          Constructs an empty hashtable with the given initial size, maximum load factor, and no maximum chain length.
HashTable(int initSize, double loadFactor, int maxChainLength)
          Constructs an empty hashtable with the given initial size, maximum load factor, and maximum chain length.
 
Method Summary
 T delete(T item)
          Removes and returns the given item from the hashtable.
 void displayStats(java.io.PrintStream out)
          Prints statistics about the hashtable to the PrintStream supplied.
 void dump(java.io.PrintStream out)
          Prints all the items in the hashtable to the PrintStream supplied.
 void insert(T item)
          Inserts the given item into the hashtable.
 T lookup(T item)
          Determines if the given item is in the hashtable and returns it if present.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HashTable

public HashTable(int initSize,
                 double loadFactor)
Constructs an empty hashtable with the given initial size, maximum load factor, and no maximum chain length. The load factor should be a real number greater than 0.0 (not a percentage). For example, to create a hash table with an initial size of 10 and a load factor of 0.85, one would use: HashTable ht = new HashTable(10, 0.85);

Parameters:
initSize - the initial size of the hashtable.
loadFactor - the load factor expressed as a real number.
Throws:
java.lang.IllegalArgumentException - if initSize is less than or equal to 0 or if loadFactor is less than or equal to 0.0

HashTable

public HashTable(int initSize,
                 double loadFactor,
                 int maxChainLength)
Constructs an empty hashtable with the given initial size, maximum load factor, and maximum chain length. The load factor should be a real number greater than 0.0 (and not a percentage). For example, to create a hash table with an initial size of 10, a load factor of 0.85, and a maximum chain length of 20, one would use: HashTable ht = new HashTable(10, 0.85, 20);

Parameters:
initSize - the initial size of the hashtable.
loadFactor - the load factor expressed as a real number.
maxChainLength - the maximum chain length.
Throws:
java.lang.IllegalArgumentException - if initSize is less than or equal to 0 or if loadFactor is less than or equal to 0.0 or if maxChainLength is less than or equal to 0.
Method Detail

lookup

public T lookup(T item)
Determines if the given item is in the hashtable and returns it if present. If more than one copy of the item is in the hashtable, the first copy encountered is returned.

Parameters:
item - the item to search for in the hashtable.
Returns:
the item if it is found and null if not found.

insert

public void insert(T item)
Inserts the given item into the hashtable. The item cannot be null. If there is a collision, the item is added to the end of the chain.

If the load factor of the hashtable after the insert would exceed (not equal) the maximum load factor (given in the constructor), then the hashtable is resized. If the maximum chain length of the hashtable after insert would exceed (not equal) the maximum chain length (given in the constructor), then the hashtable is resized. When resizing, to make sure the size of the table is reasonable, the new size is always 2 x old size + 1. For example, size 101 would become 203. (This guarantees that it will be an odd size.)

Note that duplicates are allowed.

Parameters:
item - the item to add to the hashtable.
Throws:
java.lang.NullPointerException - if item is null.

delete

public T delete(T item)
Removes and returns the given item from the hashtable. If the item is not in the hashtable, null is returned. If more than one copy of the item is in the hashtable, only the first copy encountered is removed and returned.

Parameters:
item - the item to delete in the hashtable.
Returns:
the removed item if it was found and null if not found.

dump

public void dump(java.io.PrintStream out)
Prints all the items in the hashtable to the PrintStream supplied. The items are printed in the order determined by the index of the hashtable where they are stored (starting at 0 and going to (table size - 1)). The values at each index are printed according to the order in the LinkedList starting from the beginning.

Parameters:
out - the place to print all the output.

displayStats

public void displayStats(java.io.PrintStream out)
Prints statistics about the hashtable to the PrintStream supplied. The statistics displayed are:

Parameters:
out - the place to print all the output.