TreeMap 在刚刚更改的字段中返回空值
我正在尝试计算 TreeMap
中单词的频率。我正在读取一个文件并将行传递给 StringTokenizer,然后将其逐字转换为字符串(当前字)。
如果 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);
}
I'm trying to count the frequency of words in a TreeMap
. I'm reading a file and passing the lines to a StringTokenizer
and converting it afterwards to a string word by word (currentword).
If currentword = "one"
then it puts it on the map, but if the second word is one
again instead of fetching frequency
= 1 it gets null
again!
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来它对我来说工作得很好:
输出:
看看你是否能想出一个类似的简短但完整的程序来演示你的问题 - 可能通过逐渐将你的“真实”代码削减为类似的东西。
Looks like it works fine to me:
Output:
See if you can come up with a similar short but complete program which demonstrates your problem - possibly by gradually cutting down your "real" code to something similar.