Take the 2-minute tour ×

I am so confused right now. I am supposed to write a program that uses a hashtable. The hashtable holds words along with their frequency of use. The class "Word" holds a counter and the string. If the word is already in the table then its frequency increases. I have been researching how to do this but am just lost. I need to be pointed in the right direction. Any help would be great.

share|improve this question

5 Answers 5

up vote 3 down vote accepted
Hashtable<String, Word> words = new Hashtable<String, Word>();

public void addWord(String s) {
    if (words.containsKey(s) {
    } else {
        words.put(s, new Word(s));

This will do it.

share|improve this answer
Thank you very much! –  Bill Oct 27 '11 at 0:48

Hashtable would be an unusual choice for any new Java code these days. I assume this is some kind of exercise.

I would be slightly concerned by any exercise that hadn't been updated to use newer mechanisms.

HashMap will give you better performance than Hashtable in any single threaded scenario.

But as Emmanuel Bourg points out, Bag will do all of this for you without needing the Word class at all: just add String objects to the Bag, and the bag will automatically keep count for you.

Anyway, you're being asked to use a Map, and a map lets you find things quickly by using a key. The key can be any Object, and Strings are very commonly used: they are immutable and have good implementations of hashCode and equals, which make them ideal keys.

The javadoc for Map talks about how you use maps. Hashtable is one implementation of this interface, though it isn't a particularly good one.

You need a good key to let you find existing Word objects quickly, so that you can increment the counter. While you could make the Word object itself into the key, you would have some work to do: better is to use the String that the Word contains as the key.

You find whether the Word is already in the map by looking for the value object that has the String as its key.

share|improve this answer
Thank you very much, that makes things much more clear! –  Bill Oct 27 '11 at 0:47

You'd better use a Bag, it keeps the count of each element:


share|improve this answer

This piece of code should solve your problem

  Hashtable <String, Word> myWords = new Hashtable<String, Word>();

  Word w = new Word("test");
  Word w = new Word("anotherTest");

  String inputWord = "test";

  if (myWords.containsKey(inputWord)){
share|improve this answer

Given that the class Word has a counter and a string, I'd use a HashMap<String, Word>. If your input is an array of Strings, you can accomplish something like this by using:

public Map<String, Word> getWordCount(String[] input) {
    Map<String, Word> output = new HashMap<String, Word>();

    for (String s : input) {
        Word w = output.get(s);
        if (w == null) {
            w = new Word(s, 0);
        w.incrementValue(); // Or w = new Word(s, w.getCount() + 1) if you have no such function
        output.put(s, w);

    return output;


share|improve this answer

Your Answer


