The Java Course provides a general introduction to programming in Java. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.


The Built-In Hashtable

Java provides an implementation of the Table ADT called Hashtable. It is in the java.util package. Later in the chapter we'll see why it is called Hashtable.

To demonstrate the use of the Hashtable we'll write a short program that traverses a String and counts the number of times each word appears.

We'll create a new class called WordCount that will build the Table and then print its contents. Naturally, each WordCount object contains a Hashtable:

public class WordCount {
    Hashtable ht;

    public WordCount () {
        ht = new Hashtable ();
    }
}

The only public methods for WordCount are processLine, which takes a String and adds its words to the Table, and print, which prints the results at the end.

processLine breaks the String into words using a StringTokenizer and passes each word to processWord.

    public void processLine (String s) {
        StringTokenizer st = new StringTokenizer (s, " ,.");
        while (st.hasMoreTokens()) {
            String word = st.nextToken();
            processWord (word.toLowerCase ());
        }
    }

The interesting work is in processWord.

    public void processWord (String word) {
        if (ht.containsKey (word)) {
            Integer i = (Integer) ht.get (word);
            Integer j = new Integer (i.intValue() + 1);
            ht.put (word, j);
        } else {
            ht.put (word, new Integer (1));
        }
    }

If the word is already in the table, we get its counter, increment it, and put the new value. Otherwise, we just put a new entry in the table with the counter set to 1.

To print the entries in the table, we need to be able to traverse the keys in the table. Fortunately, the Hashtable implementation provides a method, keys, that returns an Enumeration object we can use. Enumerations are very similar to the Iterators we saw in Section 17.11. Both are abstract classes in the java.util package; you should review the documentation of both. Here's how to use keys to print the contents of the Hashtable:

    public void print () {
        Enumeration enum = ht.keys ();
        while (enum.hasMoreElements ()) {
            String key = (String) enum.nextElement ();
            Integer value = (Integer) ht.get (key);
            System.out.println ("{ " + key + ", " + value + " }");
        }
    }

Each of the elements of the Enumeration is an Object, but since we know they are keys, we typecast them to be Strings. When we get the values from the Table, they are also Objects, but we know they are counters, so we typecast them to be Integers.

Finally, to count the words in a string:

        WordCount wc = new WordCount ();
        wc.processLine ("da doo ron ron ron, da doo ron ron");
        wc.print ();

The output is

{ ron, 5 }
{ doo, 2 }
{ da, 2 }

The elements of the Enumeration are not in any particular order. The only guarantee is that all the keys in the table will appear.



Last Update: 2011-01-24