Java Reference
In-Depth Information
public V put(K key, V value);
public V remove (Object key);
public V get(Object key);
public boolean containsKey(Object key);
public boolean containsValue(Object value);
public Set<K> keySet();
public Collection<V> values();
public boolean isEmpty();
public int size();
public void clear();
Notice the differences in the names of the methods. Map uses the method names put , get ,
containsKey , and size instead of our names add , getValue , contains , and getSize . Map also has
the additional method containsValue that finds out whether a dictionary contains a given value.
Instead of our methods getKeyIterator and getValueIterator that return iterators to a dic-
tionary's keys and values, respectively, Map specifies the method keySet , which returns a set of
keys, and the method values , which returns a collection of values. The Java Class Library contains
the interfaces Set and Collection , and each of these interfaces has a method iterator that returns
an iterator to the values in the corresponding ADT.
Duplicate search keys are not permitted in a dictionary that conforms to the Map interface. Each
key must correspond to only one value. Also, some of Map 's methods use Object as the data type of
the search key, whereas we use the more specific generic data type K .
C HAPTER S UMMARY
The entries in the ADT dictionary each contain two parts: a search key and a value associated with that key.
The dictionary identifies its entries by their search keys.
Dictionaries can organize their search keys in either sorted or unsorted order. The search keys can be either
distinct or duplicate.
You can add an entry to a dictionary given its search key and value. You can retrieve or remove an entry
given only its search key. By using an iterator, you can traverse all the keys or all the values in a dictionary.
An English dictionary, a directory of telephone numbers, an address book, and a library catalog are common
examples of dictionaries.
The Java Class Library contains the interface Map , which is similar to our DictionaryInterface .
P ROGRAMMING T IPS
The class Scanner enables you to break a string into substrings, or tokens, that are separated by characters
called delimiters. By default, white-space characters are the delimiters. You pass to Scanner ' s constructor
either the string to be parsed or a text file represented as an instance of the class java.io.File .
The following methods in the class Scanner enable you to extract the tokens from any string:
public String next();
public boolean hasNext();
Appendix A discusses Scanner in more detail beginning at Segment A.81.
When using a Scanner object to process text, any character can be a delimiter, if it does not occur in any
desired token. You create a string of these delimiters using a special notation and give it to the Scanner
method useDelimiter . Consult Segment A.82 of Appendix A for more details.
 
 
Search WWH ::




Custom Search