structure5
Class Hashtable<K,V>

java.lang.Object
  extended by structure5.Hashtable<K,V>
All Implemented Interfaces:
java.lang.Iterable<V>, Map<K,V>

public class Hashtable<K,V>
extends java.lang.Object
implements Map<K,V>, java.lang.Iterable<V>

Implements a dictionary as a table of hashed key-value pairs. Collisions are resolved through linear probing. Values used as keys in this structure must have a hashcode method that returns the same value when two keys are "equals". Initially, a table of suggested size is allocated. It will be expanded as the load factor (ratio of pairs to entries) grows to meet maximumLoadFactor.

Example Usage:

To create a hashtable by reading a collection of words and definitions from System.in we could use the following:

 public static void main (String[] argv){
      Hashtable dict = new Hashtable();
      ReadStream r = new ReadStream();
      String word, def;
      System.out.println("Enter a word: ");
      while(!r.eof()){
          word = r.readLine();
          System.out.println("Enter a definition: ");
          def = r.readLine();
          dict.put(word,def);
          System.out.println("Enter a word: ");
      }
      System.out.println(dict);
 }
 

See Also:
ChainedHashtable

Field Summary
protected  int count
          The number of key-value pairs in table.
protected  Vector<HashAssociation<K,V>> data
          The data associated with the hashtable.
protected  double maximumLoadFactor
          The maximum load factor that causes rehashing of the table.
protected static java.lang.String RESERVED
          A single key-value pair to be used as a token indicating a reserved location in the hashtable.
 
Constructor Summary
Hashtable()
          Construct a hash table that is initially empty.
Hashtable(int initialCapacity)
          Construct a hash table that is capable of holding at least initialCapacity values.
 
Method Summary
 void clear()
          Remove all key-value pairs from hashtable.
 boolean containsKey(K key)
          Returns true iff a specific key appears within the table.
 boolean containsValue(V value)
          Returns true if a specific value appears within the table.
 Set<Association<K,V>> entrySet()
           
protected  void extend()
           
 V get(K key)
          Get the value associated with a key.
 boolean isEmpty()
          Determine if table is empty.
 java.util.Iterator<V> iterator()
          Returns a traversal that traverses over the values of the hashtable.
 java.util.Iterator<K> keys()
          Get a traversal over the keys of the hashtable.
 Set<K> keySet()
           
protected  int locate(K key)
           
 V put(K key, V value)
          Place a key-value pair within the table.
 void putAll(Map<K,V> other)
          Put all of the values found in another map into this map, overriding previous key-value associations.
 V remove(K key)
          Remove a key-value pair from the table.
 int size()
          Return the number of key-value pairs within the table.
 java.lang.String toString()
          Generate a string representation of the hash table.
 Structure<V> values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface structure5.Map
equals, hashCode
 

Field Detail

RESERVED

protected static final java.lang.String RESERVED
A single key-value pair to be used as a token indicating a reserved location in the hashtable. Reserved locations are available for insertion, but cause collisions on lookup.

See Also:
Constant Field Values

data

protected Vector<HashAssociation<K,V>> data
The data associated with the hashtable.


count

protected int count
The number of key-value pairs in table.


maximumLoadFactor

protected final double maximumLoadFactor
The maximum load factor that causes rehashing of the table.

See Also:
Constant Field Values
Constructor Detail

Hashtable

public Hashtable(int initialCapacity)
Construct a hash table that is capable of holding at least initialCapacity values. If that value is approached, it will be expanded appropriately. It is probably best if the capacity is prime. Table is initially empty.

Parameters:
initialCapacity - The initial capacity of the hash table.
Precondition:
initialCapacity > 0
Postcondition:
constructs a new Hashtable holding initialCapacity elements

Hashtable

public Hashtable()
Construct a hash table that is initially empty.

Postcondition:
constructs a new Hashtable
Method Detail

clear

public void clear()
Remove all key-value pairs from hashtable.

Specified by:
clear in interface Map<K,V>
Postcondition:
removes all elements from Hashtable

size

public int size()
Return the number of key-value pairs within the table.

Specified by:
size in interface Map<K,V>
Returns:
The number of key-value pairs currently in table.
Postcondition:
returns number of elements in hash table

isEmpty

public boolean isEmpty()
Determine if table is empty.

Specified by:
isEmpty in interface Map<K,V>
Returns:
True if table is empty.
Postcondition:
returns true iff hash table has 0 elements

containsValue

public boolean containsValue(V value)
Returns true if a specific value appears within the table.

Specified by:
containsValue in interface Map<K,V>
Parameters:
value - The value sought.
Returns:
True iff the value appears within the table.
Precondition:
value is non-null Object
Postcondition:
returns true iff hash table contains value

containsKey

public boolean containsKey(K key)
Returns true iff a specific key appears within the table.

Specified by:
containsKey in interface Map<K,V>
Parameters:
key - The key sought.
Returns:
True iff the key sought appears within table.
Precondition:
key is a non-null Object
Postcondition:
returns true if key appears in hash table

iterator

public java.util.Iterator<V> iterator()
Returns a traversal that traverses over the values of the hashtable.

Specified by:
iterator in interface java.lang.Iterable<V>
Returns:
A value traversal, over the values of the table.
Postcondition:
returns traversal to traverse hash table

get

public V get(K key)
Get the value associated with a key.

Specified by:
get in interface Map<K,V>
Parameters:
key - The key used to find the desired value.
Returns:
The value associated with the desired key.
Precondition:
key is non-null Object
Postcondition:
returns value associated with key, or null

keys

public java.util.Iterator<K> keys()
Get a traversal over the keys of the hashtable.

Returns:
a traversal over the key values appearing within table.
Postcondition:
returns traversal to traverse the keys of hash table;

locate

protected int locate(K key)

put

public V put(K key,
             V value)
Place a key-value pair within the table.

Specified by:
put in interface Map<K,V>
Parameters:
key - The key to be added to table.
value - The value associated with key.
Returns:
The old value associated with key if previously present.
Precondition:
key is non-null object
Postcondition:
key-value pair is added to hash table

putAll

public void putAll(Map<K,V> other)
Put all of the values found in another map into this map, overriding previous key-value associations.

Specified by:
putAll in interface Map<K,V>
Parameters:
other - is the source mapping
Precondition:
other map is valid
Postcondition:
this hashtable is augmented by the values found in other

remove

public V remove(K key)
Remove a key-value pair from the table.

Specified by:
remove in interface Map<K,V>
Parameters:
key - The key of the key-value pair to be removed.
Returns:
The value associated with the removed key.
Precondition:
key is non-null object
Postcondition:
removes key-value pair associated with key

extend

protected void extend()
Postcondition:
expands the hashtable to reduce loading

entrySet

public Set<Association<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>
Postcondition:
returns a set of Associations associated with this Map

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>
Postcondition:
returns a Set of keys used in this Map

values

public Structure<V> values()
Specified by:
values in interface Map<K,V>
Postcondition:
returns a Structure that contains the (possibly repeating) values of the range of this map.

toString

public java.lang.String toString()
Generate a string representation of the hash table.

Overrides:
toString in class java.lang.Object
Returns:
The string representing the table.
Postcondition:
returns a string representation of hash table