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 ++;
}
精彩评论