public class Table
extends java.lang.Object
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.
Java Source Code for this class:
http://www.cs.colorado.edu/~main/edu/colorado/collections/Table.java
Constructor and Description |
---|
Table(int capacity)
Initialize an empty table with a specified capacity.
|
Modifier and Type | Method and Description |
---|---|
boolean |
containsKey(java.lang.Object key)
Determines whether a specified key is in this table.
|
java.lang.Object |
get(java.lang.Object key)
Retrieves an object for a specified key.
|
java.lang.Object |
put(java.lang.Object key,
java.lang.Object element)
Add a new element to this table, using the specified key.
|
java.lang.Object |
remove(java.lang.Object key)
Removes an object for a specified key.
|
public Table(int capacity)
capacity
- the capacity for this new open-address hash table
Postcondition:
This table is empty and has the specified capacity.java.lang.OutOfMemoryError
- Indicates insufficient memory for the specified capacity.public boolean containsKey(java.lang.Object key)
key
- the non-null key to look for
Precondition:
key
cannot be null.true
(if this table contains an object with the specified
key); false
otherwise. Note that key.equals( )
is used to compare the key
to the keys that are in the
table.java.lang.NullPointerException
- Indicates that key
is null.public java.lang.Object get(java.lang.Object key)
key
- the non-null key to look for
Precondition:
key
cannot be null.key
(if this
table contains an such an object); null otherwise. Note that
key.equals( )
is used to compare the key
to the keys that are in the table.java.lang.NullPointerException
- Indicates that key
is null.public java.lang.Object put(java.lang.Object key, java.lang.Object element)
key
- the non-null key to use for the new elementelement
- 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.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.java.lang.IllegalStateException
- Indicates that there is no room for a new object in this table.java.lang.NullPointerException
- Indicates that key
or element
is null.public java.lang.Object remove(java.lang.Object key)
key
- the non-null key to look for
Precondition:
key
cannot be null.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.java.lang.NullPointerException
- Indicates that key
is null.