Java Reference
In-Depth Information
The plan is that we shall have a set of words that are likely to occur in typical questions and we
will associate these words with particular responses. If the input from the user contains one of
our known words, we can generate a related response. This is still a very crude method, because
it does not pick up any of the meaning of the user's input, nor does it recognize a context, but it
can be surprisingly effective. And it is a good next step.
To do this, we will use a HashMap . You will find the documentation for the class HashMap in
the Java library documentation. HashMap is a specialization of a Map , which you will also find
documented. You will see that you need to read the documentation of both to understand what a
HashMap is and how it works.
Exercise 5.23 What is a HashMap ? What is its purpose and how do you use it? Answer
these questions in writing, and use the Java library documentation of Map and HashMap for
your responses. Note that you will find it hard to understand everything, as the documentation
for these classes is not very good. We will discuss the details later in this chapter, but see what
you can find out on your own before reading on.
Exercise 5.24 HashMap is a parameterized class. List those of its methods that depend
on the types used to parameterize it. Do you think the same type could be used for both of its
parameters?
5.6.1
The concept of a map
A map is a collection of key/value pairs of objects. As with the ArrayList , a map can store a
flexible number of entries. One difference between the ArrayList and a Map is that with a Map
each entry is not an object, but a pair of objects. This pair consists of a key object and a value
object.
Concept:
A map is a col-
lection that stores
key/value pairs as
entries. Values can
be looked up by
providing the key.
Instead of looking up entries in this collection using an integer index (as we did with the
ArrayList ), we use the key object to look up the value object.
An everyday example of a map is a telephone directory. A telephone directory contains entries,
and each entry is a pair: a name and a phone number. You use a phone book by looking up a
name and getting a phone number. We do not use an index—the position of the entry in the
phone book—to find it.
A map can be organized in such a way that looking up a value for a key is easy. In the case of a
phone book, this is done using alphabetical sorting. By storing the entries in the alphabetical or-
der of their keys, finding the key and looking up the value is easy. Reverse lookup (finding the
key for a value—i.e., finding the name for a given phone number) is not so easy with a map. As
with a phone book, reverse lookup in a map is possible, but it takes a comparatively long time.
Thus, maps are ideal for a one-way lookup, where we know the lookup key and need to know a
value associated with this key.
5.6.2
Using a HashMap
HashMap is a particular implementation of Map . The most important methods of the HashMap
class are put and get .
 
Search WWH ::




Custom Search