next up previous index
Next: Comment Up: Hash Tables Previous: The Object hash function   Index

Lookup tables

So far we have said nothing about the implementation of hash tables in Java. To be specific, we shall consider a hash table implementation of a certain interface called a LookupTable.

The idea of a lookup table is that each entry should store a pair of objects, one of which is the key and the other is a corresponding value. For example you could consider a dictionary to be a lookup table, where the key is a word and the value is the dictionary definition of that word. Again, if the lookup table records the membership of a club, the key might be the member's name and the value might be the member's address, membership number, status etc. Here is simple LookupTable interface.

public interface LookupTable {
    public boolean isEmpty();
    public int size();
    public boolean contains(Object key);
    public void add(Object key, Object value);
    public void remove(Object key);
    public Object lookup(Object key);
    public interface Entry {
        public Object key();
        public Object value();
    }
    public Iterator iterator();
}
The meanings of most of the LookupTable methods are fairly clear from their names.
size
returns the number of entries in the table.
contains
returns true or false depending on whether or not the table contains an entry for this key.
add
inserts the specified value into the table using the specified key. If the key is already present, its value is overwritten.
remove
removes the entry corresponding to the given key, and does nothing if the key is not present.
lookup
returns the value associated with a given key. If the key is not present, it throws a NoSuchElementException.
iterator
returns an iterator over the entries in the table. The idea is that the iterator.next method should return a table entry. Since an entry is a compound object consisting of a key and a value, we define a local LookupTable.Entry interface for such a pair, together with methods for extracting its two fields. The only way to obtain a reference to an Entry is to have one returned from the iterator.next method.



Subsections
next up previous index
Next: Comment Up: Hash Tables Previous: The Object hash function   Index
Peter Williams 2005-06-07