Nanda
Nanda

Reputation: 1058

to find the frequency of words in a text file using java

Ive managed to parse the entire contents of a given input text file and store each word in a hash set. But now i need to find the frequenct of each of these words in this input file, any suggestions as to how I can go about? :)

Upvotes: 0

Views: 1757

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328624

Use a HashMap instead of a HashSet and this class as the value:

class Counter {
    public int frequency;
}

addWord() then looks like this:

public void addWord (String word) {
    Counter c = map.get (word);
    if (c == null) {
        c = new Counter ();
        map.put(word, c);
    }
    c.frequency ++;
}

Upvotes: 1

Related Questions