词频统计
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class util {
public static void main(String[] args) throws IOException {
// io流 BufferedReader 字符缓冲流 InputStreamReader 字节流转字符流 FileInputStream 字节流
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:/wcinput/wc.txt")));
//map 存储键值对
Map<String, Integer> map = new HashMap<>();
String line = "";
while ((line = br.readLine()) != null) {
String[] arr = line.split(" ");
for (String word : arr) {
if (map.get(word) == null) {
map.put(word, 1);
} else {
map.put(word, map.get(word) + 1);
}
}
}
//map存储最终的结果
System.out.println(map);
//map集合中keySet() 把所有的key放到set集合中 遍历map集合中所有的key
Set<String> set = map.keySet();
for (String str : set) {
System.out.println(str + " " + map.get(str));
}
}
}