Java Reference
In-Depth Information
getValueIterator()
+getValueIterator() : Iterator<V>
Task: Creates an iterator that traverses all values
in the dictionary.
Input: None.
Output: Returns an iterator that provides
sequential access to the values in
the dictionary.
isEmpty()
+isEmpty() : boolean
Task: Sees whether the dictionary is empty.
Input: None.
Output: Returns true if the dictionary is empty.
getSize()
+getSize() : integer
Task: Gets the size of the dictionary.
Input: None.
Output: Returns the number of entries (key-
value pairs) currently in the dictionary.
clear()
+clear() : void
Task: Removes all entries from the dictionary.
Input: None.
Output: None.
19.3
Refining the specifications. Even though all dictionaries can have this common set of operations,
you do need to refine some of the specifications according to whether a dictionary's search keys are
distinct:
Distinct search keys. The method add can ensure that the search keys in a dictionary are dis-
tinct. If key is already in the dictionary, the operation add(key, value) could either refuse to
add another key-value entry or change the existing value associated with key to value . In the
latter case, the method could return the old replaced value instead of not having an output, as
indicated earlier.
Regardless of how add guarantees distinct search keys, the remaining methods can have
simpler implementations than if duplicate search keys are allowed. For example, the methods
remove and getValue will either find the one value associated with a given search key or dis-
cover that no such entry exists.
Duplicate search keys. If the method add adds every given key-value entry to a dictionary,
the methods remove and getValue must deal with multiple entries that have the same search
key. Which entry should be removed or returned? The method remove could either remove
the first value it finds or remove all values associated with the given search key. If getValue
returns an object, it could return the first value it finds. Or you could modify getValue to
return a list of values, for example.
Another possibility is to have a secondary search key that is used only when several
entries have the same primary search key. For example, if you call directory assistance for a
common name like John Smith, you most certainly will be asked for John's address.
For simplicity, we will assume distinct search keys and consider duplicate search keys in the exer-
cises and projects at the end of this chapter.
 
Search WWH ::




Custom Search