All Packages Class Hierarchy This Package Previous Next Index
Class rpi.goldsd.container.Table
java.lang.Object
|
+----rpi.goldsd.container.Table
- public class Table
- extends Object
- implements AssociativeContainer
The Table class provides a generic associative hashtable by
implementing the AssociativeContainer interface. Objects
are identified by a unique key object that is also used in determining
the corresponding hash values. The current implementation of the
hashtable consists of an array of references to LinkedList objects.
One of the current shortcomings of this class is the implicit
requirement that the Comparable key objects also implement
the Hashable interface. Thus, instead of a compilation error
indicating that a key must be Hashable, a run-time exception
occurs.
- Version:
- 1.3, 3/27/98
- Author:
- David Goldschmidt
- See Also:
- LinkedList, Comparable, Hashable, DuplicateElementException, EmptyEnumeration
-
DEFAULT_SIZE
- The default table size used by the default constructor.
-
numElements
- The number of associated elements in this hashtable.
-
table
- The array of references to LinkedList objects.
-
Table()
- Constructs a hashtable with the default table size.
-
Table(int)
- Constructs a hashtable with the specified table size.
-
add(Comparable)
- Adds a new associative entry to this hashtable.
-
add(Comparable, Object)
- Adds a new associative entry to this hashtable.
-
clear()
- Removes all elements from this hashtable.
-
contains(Comparable)
- Returns true if the given key maps to an element in the
associative container.
-
elements()
- Provides an enumeration of the elements in this hashtable.
-
elements(int)
- Provides an enumeration of the elements in this hashtable for
a specific hash value (i.e.
-
getSize()
- Returns the number of elements in this hashtable.
-
getTableSize()
- Returns the size of the hashtable.
-
hash(Comparable)
- Determines the hash value based on the Comparable key.
-
isEmpty()
- Returns true if this hashtable contains no elements.
-
keys()
- Provides an enumeration of the keys stored in this hashtable.
-
map(Comparable)
- Provides a mechanism for mapping a given key to its corresponding data.
-
remove(Comparable)
- Removes a data element from this hashtable identified by the
given key.
-
toString()
- The toString() method is the default method used to display
the keys and corresponding data elements of this hashtable.
DEFAULT_SIZE
public static final int DEFAULT_SIZE
- The default table size used by the default constructor.
numElements
protected int numElements
- The number of associated elements in this hashtable.
table
protected LinkedList table[]
- The array of references to LinkedList objects.
Table
public Table(int tableSize)
- Constructs a hashtable with the specified table size.
- Parameters:
- tableSize - the number of hash "buckets" to construct.
Table
public Table()
- Constructs a hashtable with the default table size.
add
public synchronized void add(Comparable keyAndData) throws DuplicateElementException, IllegalArgumentException
- Adds a new associative entry to this hashtable. If the
Comparable key parameter does not also implement the
Hashable interface (which is a sub-interface of the
Comparable interface), then an IllegalArgumentException
is thrown.
This method performs in O(n) time in the worst-case,
where n is the number of elements in the hashtable.
Inherent in hashing, the average performance is significantly better
since the linked-list used to store the data should be very
small with an evenly distributed hash function.
- Parameters:
- keyAndData - the unique key used to identify the associated
data which is simply the Comparable argument.
- Throws: IllegalArgumentException
- if the given key
object does not implement the Hashable interface.
- Throws: DuplicateElementException
- if the key used already
exists in this table.
add
public synchronized void add(Comparable key,
Object data) throws DuplicateElementException, IllegalArgumentException
- Adds a new associative entry to this hashtable. If the
Comparable key parameter does not also implement the
Hashable interface (which is a sub-interface of the
Comparable interface), then an IllegalArgumentException
is thrown.
This method performs in O(n) time in the worst-case,
where n is the number of elements in the hashtable.
Inherent in hashing, the average performance is significantly better
since the linked-list used to store the data should be very
small with an evenly distributed hash function.
- Parameters:
- key - the unique key used to identify the associated data.
- data - the actual data to be stored.
- Throws: IllegalArgumentException
- if the given key
object does not implement the Hashable interface.
- Throws: DuplicateElementException
- if the key used already
exists in this table.
clear
public synchronized void clear()
- Removes all elements from this hashtable.
This method performs in constant time O(1).
contains
public boolean contains(Comparable key)
- Returns true if the given key maps to an element in the
associative container. This method makes use of the map()
method.
This method performs in O(n) in the worst-case; however,
performance is substantially better in the average case, provided
that the hash function provides an even distribution of hash values.
- Parameters:
- key - the unique key used to identify an element in the
associative container.
- Returns:
- true if the given key maps to an element in
the associative container; false otherwise.
elements
public synchronized Enumeration elements()
- Provides an enumeration of the elements in this hashtable.
This method performs in constant time O(1).
- Returns:
- an enumeration of the elements stored in this hashtable.
elements
public synchronized Enumeration elements(int hashValue)
- Provides an enumeration of the elements in this hashtable for
a specific hash value (i.e. an enumeration of the elements in
the linked list for the given hash value).
This method performs in constant time O(1).
- Parameters:
- hashValue - the hash value to look up.
- Returns:
- an enumeration of the elements stored in this hashtable
for a specific hash value.
getSize
public synchronized int getSize()
- Returns the number of elements in this hashtable.
This method performs in constant time O(1).
- Returns:
- the number of elements in this hashtable.
getTableSize
public synchronized int getTableSize()
- Returns the size of the hashtable.
This method performs in constant time O(1).
- Returns:
- the size of the hashtable.
hash
protected synchronized int hash(Comparable key)
- Determines the hash value based on the Comparable key.
This method performs in constant time O(1).
- Throws: IllegalArgumentException
- if the given key object
does not implement the Hashable interface.
isEmpty
public synchronized boolean isEmpty()
- Returns true if this hashtable contains no elements.
This method performs in constant time O(1).
- Returns:
- true if this hashtable contains no elements;
false otherwise.
keys
public synchronized Enumeration keys()
- Provides an enumeration of the keys stored in this hashtable.
This method performs in constant time O(1).
- Returns:
- an enumeration of the keys stored in this hashtable.
map
public synchronized Object map(Comparable key)
- Provides a mechanism for mapping a given key to its corresponding data.
This method performs in O(n) time in the worst case,
where n is the number of elements in the hashtable.
Inherent in hashing, the average performance is substantially better
since the linked-list used to store the data should be very
small with an evenly distributed hash value.
- Parameters:
- key - the key used to identify and locate the associated data.
- Returns:
- the data stored in this hashtable identified by the
given key; or null if the given key
does not identify an element in this hashtable.
- Throws: IllegalArgumentException
- if the given key object
does not implement the Hashable interface.
remove
public synchronized Object remove(Comparable key)
- Removes a data element from this hashtable identified by the
given key.
This method performs in O(n) time in the worst-case,
where n is the number of elements in the hashtable.
Inherent in hashing, the average performance is substantially better
since the linked-list used to store the data should be very
small with an evenly distributed hash value.
- Parameters:
- key - the key used to identify and locate the associated data.
- Returns:
- the object that is removed from this hashtable.
- Throws: IllegalArgumentException
- if the given key object
does not implement the Hashable interface.
toString
public synchronized String toString()
- The toString() method is the default method used to display
the keys and corresponding data elements of this hashtable.
This method performs in O(n) time since each individual
node of the hashtable must be visited once.
- Returns:
- a String representing the keys and corresponding data
elements of this hashtable.
- Overrides:
- toString in class Object
All Packages Class Hierarchy This Package Previous Next Index