Java Reference
In-Depth Information
A Java Interface
19.4
Listing 19-1 contains a Java interface for the ADT dictionary that specifies distinct search keys.
The add method replaces the value associated with any search key that is already in the dictionary.
Like the interfaces for the ADTs list and sorted list, this interface specifies the data type of its
entries generically. Since the search keys can have a data type that differs from the type of the asso-
ciated values, we use two formal type parameters, K and V . K represents the data type of the search
keys, and V is the type of the associated values.
LISTING 19-1
An interface for the ADT dictionary
import java.util.Iterator;
/**
An interface for a dictionary with distinct search keys.
@author Frank M. Carrano
*/
public interface DictionaryInterface<K, V>
{
/** Adds a new entry to this dictionary. If the given search
key already exists in the dictionary, replaces the
corresponding value.
@param key an object search key of the new entry
@param value an object associated with the search key
@return either null if the new entry was added to the dictionary
or the value that was associated with key if that value
was replaced */
public V add(K key, V value);
/** Removes a specific entry from this dictionary.
@param key an object search key of the entry to be removed
@return either the value that was associated with the search key
or null if no such object exists */
public V remove(K key);
/** Retrieves from this dictionary the value associated with a given
search key.
@param key an object search key of the entry to be retrieved
@return either the value that is associated with the search key
or null if no such object exists */
public V getValue(K key);
/** Sees whether a specific entry is in this dictionary.
@param key an object search key of the desired entry
@return true if key is associated with an entry in the
dictionary */
public boolean contains(K key);
/** Creates an iterator that traverses all search keys in this
dictionary.
 
 
Search WWH ::




Custom Search