创建文档
编程实现:
package week16;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
/**
* @功能:词频统计
* @作者:罗燕
* @日期:2022-06-09
*/
public class WordCount {
public static void main(String[] args) throws Exception {
BufferedReader br =new BufferedReader(new FileReader("words"));
Map<String,Integer>wc=new HashMap<>();
String nextLine="";
while ((nextLine= br.readLine())!=null){
//按空格拆分
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)+")");
}
}
}
运行代码,查看结果