我試圖計算TreeMap
中單詞的頻率。我正在讀取文件並將行傳遞給StringTokenizer
,然後將其轉換爲逐字(currentword)的字符串。TreeMap在剛剛更改的字段中返回空值
如果currentword = "one"
然後它把它在地圖上,但如果第二個字又是one
而不是獲取frequency
= 1再次得到null
!
final StringTokenizer parser = new StringTokenizer(currentLine, " \0\t\n\r\f.,;:!?'");
while (parser.hasMoreTokens()) {
String currentWord = parser.nextToken();
Integer frequency = frequencyMap.get(currentWord);
if (frequency == null) {
frequency = 0;
}
frequency++;
frequencyMap.put(currentWord, frequency);
}
爲什麼當前字詞上的'final'? – jere 2012-01-27 13:30:44
它不清楚你想要什麼。 – 2012-01-27 13:32:41
即時通過樹圖統計單詞的頻率 – user1173527 2012-01-27 13:33:15