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 compiler 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 something. (We do not have time to stop and explain what they mean, but a
hint is given in the starred subsection “Nonparameterized Version of the Collection
Framework.”) Your only defense against this pitfall is to be very careful; 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
mistake. 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 an omitted <Class_Name> .
Finally, we should note that sometimes your code will compile and even run
correctly 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
database 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.
Because 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 operations 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.
Abstract
Map<K,V>
Hash
Map<K,V>
Map<K,V>
interface
 
 
Search WWH ::




Custom Search