Class Table
java.lang.Object
|
+----Table
- public class Table
- extends Object
A Table is an open-address hash table with a fixed capacity.
The purpose is to show students how an open-address hash table is
implemented. Programs should generally use java.util.Hashtable
rather than this hash table.
- Version:
- Jun 12, 1998. The method
nextIndex was fixed April 1999 by
Craig Miller.
- Author:
- Michael Main
(main@colorado.edu)
-
Table(int)
- Initialize an empty table with a specified capacity.
-
containsKey(Object)
- Determines whether a specified key is in this table.
-
get(Object)
- Retrieves an object for a specified key.
-
put(Object, Object)
- Add a new element to this table, using the specified key.
-
remove(Object)
- Removes an object for a specified key.
Table
public Table(int capacity)
- Initialize an empty table with a specified capacity.
- Parameters:
-
capacity
- the capacity for this new open-address hash table
- Postcondition:
-
This table is empty and has the specified capacity.
- Throws: OutOfMemoryError
- Indicates insufficient memory for the specified capacity.
containsKey
public boolean containsKey(Object key)
- Determines whether a specified key is in this table.
- Parameters:
-
key
- the non-null key to look for
- Precondition:
-
key cannot be null.
- Returns:
-
truefalse otherwise. Note that key.equals( )
is used to compare the key to the keys that are in the
table.
- Throws: NullPointerException
- Indicates that
key is null.
get
public Object get(Object key)
- Retrieves an object for a specified key.
- Parameters:
-
key
- the non-null key to look for
- Precondition:
-
key cannot be null.
- Returns:
- a reference to the object with the specified
keykey.equals( ) is used to compare the key
to the keys that are in the table.
- Throws: NullPointerException
- Indicates that
key is null.
put
public Object put(Object key,
Object element)
- Add a new element to this table, using the specified key.
- Parameters:
-
key
- the non-null key to use for the new element
-
element
- the new element that’s being added to this table
- Precondition:
-
If there is not already an element with the specified
key,
then this table’s size must be less than its capacity
(i.e., size() < capacity()). Also, neither key
nor element is null.
- Postcondition:
-
If this table already has an object with the specified
key,
then that object is replaced by element, and the return
value is a reference to the replaced object. Otherwise, the new
element is added with the specified key
and the return value is null.
- Throws: IllegalStateException
- Indicates that there is no room for a new object in this table.
- Throws: NullPointerException
- Indicates that
key or element is null.
remove
public Object remove(Object key)
- Removes an object for a specified key.
- Parameters:
-
key
- the non-null key to look for
- Precondition:
-
key cannot be null.
- Postcondition:
-
If an object was found with the specified key, then that
object has been removed from this table and a copy of the removed object
is returned; otherwise, this table is unchanged and the null reference
is returned. Note that
key.equals( ) is used to compare the key
to the keys that are in the table.
- Throws: NullPointerException
- Indicates that key is null.