Java Reference
In-Depth Information
PITFALL: Omitting the <T>
If you omit <T> or a corresponding class name, such as using ArrayList instead of
ArrayList<String> , then you may get a compiler error message. If you do get a com-
piler error message, it is likely to seem bewilderingly strange. The problem is that
ArrayList and other class and interface names with <T> omitted actually mean some-
thing. (We do not have time to stop and explain what they mean, but a hint of what
they mean is given in the starred subsection “Nonparameterized Version of the Collec-
tion Framework.”) Your only defense against this pitfall is to be very careful, and if you
do get a bewildering compiler error message, look for a missing <T> or a missing
<Class_Name> .
Sometimes a compiler warning message can be helpful when you make this mis-
take. If you get a warning that mentions a type case from a class name without a <T> to
a class name with a <T> or with a <Class_Name> , look for an omitted <T> or omitted
<Class_Name> .
Finally, we should note that sometimes your code will compile and even run cor-
rectly if you omit the <T> from a class name in the collection framework.
16.2
Maps
A man has but one mother. But, a mother may have any number of sons.
Saying on a Wall Sampler
map
The Java map framework is similar in character to the collection framework, except
that it deals with collections of ordered pairs. Objects in the map framework can
implement mathematical functions and relations and so can be used to construct data-
base classes. Think of the pair as consisting of a key K (to search for) and an associated
value V . For example, the key might be a student ID number and the value might be an
object storing information about the student (such as the name, major, address, or
phone number) associated with that ID number. Commonly used interfaces and
classes in this framework are shown in Display 16.8. In this chapter we will focus on
the Map<K,V> interface, the AbstractMap<K,V> class, and the HashMap<K,V> class.
Since the map interface will map a key to a value, we must now specify two types of
parameters instead of one as we did with collections. The Map<K,V> interface specifies
the basic operations that all map classes should implement. A summary of these oper-
ations is given in Display 16.9. A more detailed description is in Appendix 5. Note
that there are many similarities to the Collection<T> interface.
AbstractMap
<K,V>
HashMap<K,V>
Map<K,V>
interface
Search WWH ::




Custom Search