Hash tables
A dataset of key-value pairs is usually implemented as a hash table. It is a data structure in which the key acts like an index into the set, much like page numbers in a book or line numbers in a table. This direct access is much faster than sequential access, which is like searching through a book page-by-page for a certain word or phrase.
In Java, we usually use the java.util.HashMap<Key,Value>
class to implement a key-value pair dataset. The type parameters Key
and Value
are specified classes. (There is also an older HashTable
class, but it is considered obsolete.)
Here is a data file of seven South American countries:

Figure 2-1 Countries data file
Here is a Java program that loads this data into a HashMap
object:

Listing 2-1 HashMap example for Countries data
The Countries.dat
file is in the data
folder. Line 15 instantiates a java.io.File
object named dataFile
to represent the file. Line 16 instantiates a java.util.HashMap
object named dataset
. It is structured to have...