Title: Dictionaries
1Dictionaries
2Chapter Contents
- Specifications for the ADT Dictionary
- Java Interface
- Iterators
- Applications
- Phone Book
- Frequency of Words
- Concordance of Words
- Java Class Library Map Interface
3Specifications for the ADT Dictionary
- Contains entries that each have two parts
- A key word or search key
- A value associated with the key
Fig. 17-1 An English dictionary.
4Specifications for the ADT Dictionary
- Data
- Pairs of objects (key, value)
- Number of pairs in the collection
- Operations
- add
- remove
- getValue
- contains
- getKeyIterator
- getValueIterator
- isEmpty
- isFull
- getSize
- clear
5Iterators
- Note that getKeyIterator and getValueIterator
return iterators
- Create iterators for a dictionary myTable
withIterator keyIterator myTable.getKeyIterator
()Iterator valueIterator myTable.getValueItera
tor() - An iteration of a dictionary's values
- Corresponds to an iteration of the search keys
6Iterators
Fig. 17-2 Two iterators that traverse a
dictionary's keys and values in parallel.
7Using the ADT Dictionary
- A directory of telephone numbers
Fig 17-3 A class diagram for a telephone
directory.
8A Directory of Telephone Numbers
- The beginnings of TelephoneDirectory
import java.io.import java.util.public
class TelephoneDirectory private DictionaryInt
erface phoneBook private static final String
DELIMITERS " \n\r\t" public
TelephoneDirectory() phoneBook new
SortedDictionary() . . .
9A Directory of Telephone Numbers
- Other methods may include
/ Task Reads a text file of names and
telephone numbers. _at_param dataFile a text file
that is open for input /public void
readFile(BufferedReader dataFile)
throws IOException public String getPhoneN
umber(String first, String last)
public String getPhoneNumber(Name personName)
/ Task Interacts with user /public String
getPhoneNumber()
10The Frequency of Words
- We seek an ADT that will enable us to count each
occurrence of a word as it is read from a text
file
- To determine the frequency of a word
- The word must be the search key
- The data field will be of the class
FrequencyCounter
- Read a word from the file
- If not in dictionary, add it, initialize word
count 1
- If it is in the dictionary, increment the word
count
11A Concordance of Words
- Provide an index for finding a word in a file
- Index gives line (or page) number
- A word may appear multiple times
- Appears once in index/concordance
- Associated data is a list of line (page) numbers
12Concordance...Example
- Learning without thought is labor lost
- thought without learning is perilous.
- is 1 2
- labor 1
- learning 1 2
- lost 1
- perilous 2
- thought 1 2
- without 1 2
13Java Class Library The Interface Map
- A list of method signatures in Map
- Note similarity to ADT Dictionary
- Not String-specific!
public Object put(Object key, Object
value)public Object remove(Object key)public
Object get(Object key)public boolean
containsKey(Object key)public boolean
containsValue(Object value)public Set
keySet()public Collection values()public
boolean isEmpty()public int size()public void
clear()