Java Reference
In-Depth Information
/
**
TheDictionaryabstractclass.
*
/
publicinterfaceDictionary<Key,E>{
/
**
Reinitializedictionary
*
/
publicvoidclear();
/
**
Insertarecord
@paramkThekeyfortherecordbeinginserted.
@parameTherecordbeinginserted.
*
/
publicvoidinsert(Keyk,Ee);
/
**
Removeandreturnarecord.
@paramkThekeyoftherecordtoberemoved.
@returnAmachingrecord.Ifmultiplerecordsmatch
"k",removeanarbitraryone.Returnnullifnorecord
withkey"k"exists.
*
/
publicEremove(Keyk);
/
**
Removeandreturnanarbitraryrecordfromdictionary.
@returntherecordremoved,ornullifnoneexists.
*
/
publicEremoveAny();
/
**
@returnArecordmatching"k"(nullifnoneexists).
Ifmultiplerecordsmatch,returnanarbitraryone.
@paramkThekeyoftherecordtofind
*
/
publicEfind(Keyk);
/
**
@returnThenumberofrecordsinthedictionary.
*
/
publicintsize();
};
Figure4.29
The ADT for a simple dictionary.
Figure 4.29 shows the definition for a simple abstract dictionary class. The
methods
insert
and
find
are the heart of the class. Method
insert
takes a
record and inserts it into the dictionary. Method
find
takes a key value and returns
some record from the dictionary whose key matches the one provided. If there are
multiple records in the dictionary with that key value, there is no requirement as to
which one is returned.
Method
clear
simply re-initializes the dictionary. The
remove
method is
similar to
find
, except that it also deletes the record returned from the dictionary.
Once again, if there are multiple records in the dictionary that match the desired
key, there is no requirement as to which one actually is removed and returned.
Method
size
returns the number of elements in the dictionary.
The remaining Method is
removeAny
. This is similar to
remove
, except
that it does not take a key value. Instead, it removes an arbitrary record from the
dictionary, if one exists. The purpose of this method is to allow a user the ability
to iterate over all elements in the dictionary (of course, the dictionary will become
empty in the process). Without the
removeAny
method, a dictionary user could