Question

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? :)

Was it helpful?

Solution

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 ++;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top