java-多线程-词频统计

面试遇到的机试题,统计出一篇或者多篇文章的词频数 考验java基础的掌握


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class test {
//定义单词总数
private static Integer n=0;
//存储单词,数量
private static Map<String,Integer> map = new TreeMap<String,Integer>();
/**
* 读取方法
* @param fileNamePath 文件名字
*/
public void read(String fileNamePath){
try {
BufferedReader br = new BufferedReader(new FileReader(fileNamePath));
StringBuffer sb = new StringBuffer();
String line = "";
while((line=br.readLine())!=null){
sb.append(line);
}
br.close();
//正则
Pattern p = Pattern.compile("[a-zA-Z]+");
String words = sb.toString();
Matcher matcher = p.matcher(words);
int times=0;
while(matcher.find()){
String word = matcher.group();
n++;
if(map.containsKey(word)){
times = map.get(word);
// 利用map的key统计数量
map.put(word, times+1);
}else{
map.put(word, 1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建线程
* @param fileNamePath
* @throws Exception
*/
public void creatThread(String fileNamePath) throws Exception{
Thread thread = new Thread(){
public void run(){
read(fileNamePath);
}
};
thread.start();
thread.join();
}
/**
* 排序方法
* @throws IOException
*/
public void comparatorToWord() throws IOException{
//对统计进行排序
List<Entry<String,Integer>> list = new ArrayList<Entry<String,Integer>>(map.entrySet());
Comparator<Entry<String,Integer>> com = new Comparator<Entry<String,Integer>>(){
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return (o2.getValue().compareTo(o1.getValue()));
}
};
Collections.sort(list,com);
// 写出到文件
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\read.txt"));
for(Entry<String,Integer> e:list){
System.out.println("单词  "+e.getKey()+",次数  "+e.getValue());
bw.write("单词  "+e.getKey()+",次数  "+e.getValue());
bw.newLine();
}
bw.flush();
bw.close();
}
public static void main(String[] args) throws IOException {
// 读取D盘的文件
File file = new File("D:");
test r = new test();
if(file.isDirectory()){
File[] files = file.listFiles();
// 定义文件类型
String match = "\\w+.txt";
for(int i=0;i<files.length;i++){
String fileName = files[i].getName();
if(fileName.matches(match)){
try {
System.out.println(fileName);
r.creatThread("D:\\"+fileName);
} catch (Exception e) {
e.printStackTrace();

}
}
r.comparatorToWord();
}
}
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值