主要代码:
1、工具方法类
public class WordFreqUtil {
public static List> wordFreqCount(String filePath,int sortType) throws IOException {
Map map = new TreeMap(); //存储单词计数信息,key值为单词,value为单词数
BufferedReader reader=new BufferedReader(new FileReader(filePath));
String readLine = null;
while((readLine = reader.readLine()) != null){
String [] wordArray = filterWord(readLine);
for(String word : wordArray) {
word = word.trim();
if (word != null && !word.equalsIgnoreCase("")) {
if(map.get(word) != null){
map.put(word, map.get(word) + 1);
}else{
map.put(word ,1);
}
}
}
}
return sortWordFreq(map,sortType);
}
private static List> sortWordFreq(Map map, final int sortType) {
List> list = new ArrayList>(map.entrySet());
Collections.sort(list, new Comparator>() {
@Override
public int compare(Map.Entry o1, Map.Entry o2) {
if(sortType==1){
return o1.getValue() - o2.getValue(); //升序
}else{
return o2.getValue() - o1.getValue(); //降序
}
}
});
return list;
}
public static String[] filterWord(String line){
line = line.toLowerCase();
return line.split("[^a-zA-Z]");
}
}
2、单词统计类
public class WordCount { private int count; public WordCount(int count){ this.count=count; } }