开发者

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 freq开发者_运维问答uenct of each of these words in this input file, any suggestions as to how I can go about? :)


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 ++;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜