import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
public class one{
public static void main(String[] args)
{
try {
BufferedReader br = new BufferedReader(new FileReader("D:\\test.txt"));//新建缓存区读取为所需统计文件的读取
StringBuffer mp= new StringBuffer();
String s;
;
while (( s=br.readLine())!= null) {
mp.append(s);
}
Map<String,Integer> map = new HashMap<String, Integer>();//运用哈希排序的方法进行排序
StringTokenizer st = new StringTokenizer(mp.toString(),",.! \n");//分割字符串
//用来测试是否有此标记生成器的字符串可以有更多的标记。并把分割好的单词保存在letter字符串中。
while (st.hasMoreTokens()) {
String letter = st.nextToken();
int count;
if (map.get(letter) == null) {
count = 1;//表明了没有进行分割。
} else {
count = map.get(letter).intValue() + 1;
}
map.put(letter,count);
}
Set<WordEntity> set = new TreeSet<WordEntity>();
for (String key : map.keySet()) {
set.add(new WordEntity(key,map.get(key)));
}
int count = 1;
for (Iterator<WordEntity> it = set.iterator(); it.hasNext(); ) {
WordEntity w = it.next();
if(!w.getKey().equals("the")&&!w.getKey().equals("is")&&!w.getKey().equals("to")&&!w.getKey().equals("by")&&!w.getKey().equals("is")&&!w.getKey().equals("a")&&!w.getKey().equals("and")&&!w.getKey().equals("was")&&!w.getKey().equals("has")&&!w.getKey().equals("had")&&!w.getKey().equals("I")&&!w.getKey().equals("for")&&!w.getKey().equals("my")&&!w.getKey().equals("me")&&!w.getKey().equals("with")&&!w.getKey().equals("of")&&!w.getKey().equals("in")&&!w.getKey().equals("on")&&!w.getKey().equals("that")&&!w.getKey().equals("it")&&!w.getKey().equals("The")&&!w.getKey().equals("at")&&!w.getKey().equals("which")&&!w.getKey().equals("he")&&!w.getKey().equals("as")
&&!w.getKey().equals("but")&&!w.getKey().equals("his")&&!w.getKey().equals("from")&&!w.getKey().equals("some")&&!w.getKey().equals("be")&&!w.getKey().equals("were")&&!w.getKey().equals("not") &&!w.getKey().equals("they")&&!w.getKey().equals("this")&&!w.getKey().equals("an")&&!w.getKey().equals("no")&&!w.getKey().equals("into")&&!w.getKey().equals("It")&&!w.getKey().equals("there")&&!w.getKey().equals("But")&&!w.getKey().equals("him")&&!w.getKey().equals("could")&&!w.getKey().equals("been")&&!w.getKey().equals("would")&&!w.getKey().equals("she")&&!w.getKey().equals("then")&&!w.getKey().equals("Then")&&!w.getKey().equals("have"))
{ System.out.println("第" + count + "名为单词:" + w.getKey() + " 出现的次数为: "
+ w.getCount());
if (count == 10)// 当输出10个后跳出循环
break;
count++;
}
}
} catch (FileNotFoundException e) {
System.out.println("文件未找到~!");//异常处理
} catch (IOException e) {
System.out.println("文件读异常~!");//异常处理
}
}
}
class WordEntity implements Comparable<WordEntity> {
private String key;
private Integer count;
public WordEntity (String key, Integer count) {
this.key = key;
this.count = count;
}
public int compareTo(WordEntity o) {
int cmp = count.intValue() - o.count.intValue();
return (cmp == 0 ? key.compareTo(o.key) : -cmp);
//只需在这儿加一个负号就可以决定是升序还是降序排列 -cmp降序排列,cmp升序排列
//因为TreeSet会调用WorkForMap的compareTo方法来决定自己的排序
}
public String toString() {
return key + " 出现的次数为:" + count;
}
public String getKey() {
return key;
}
public Integer getCount() {
return count;
}
}