词频统计
- 在项目根目录里创建单词文本文件 - words.txt
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2ef98f51ecf93314ad3aa64f7438b3a4.png#pic_center)
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
public class WordCount {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new FileReader("words.txt"));
Map<String, Integer> wc = new HashMap<>();
String nextLine = "";
while ((nextLine = br.readLine()) !=null) {
System.out.println(nextLine);
String [] words = nextLine.split(" ");
for (String word : words) {
wc.put(word, wc.containsKey(word) ? wc.get(word) + 1 : 1);
}
}
for (String key:wc.keySet()) {
System.out.println("(" + key + "," + wc.get(key) + ")");
}
}
}