词频统计
- 在项目根目录里创建单词文本文件 - words.txt

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) + ")");
}
}
}