1. 程式人生 > >統計文章中單詞的字數並按照出現的頻率排序(treeSet)

統計文章中單詞的字數並按照出現的頻率排序(treeSet)

基本思路:用hashmap儲存key值為單詞,value為單詞出現的頻率。

  1. hashmap儲存對單詞的遍歷
  2. map的遍歷儲存在set中
  3. 排序 TreeSet
  4. 對wordEntity實現comparable介面重寫compareTo()和toString()
  5. 增序為cmp的值,降序課為-cmp的值
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import
java.util.HashMap;
import java.util.Iterator; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeSet; public class wordCen { public static void main(String args[]){ System.out.println("請輸入檔案的地址"); Scanner in
=new Scanner(System.in); String Filepath=in.next(); File file=new File(Filepath); String buffer; Map<String,Integer> map=new HashMap<String,Integer>(); try { BufferedReader reader=new BufferedReader(new FileReader(file)); while
((buffer=reader.readLine())!=null){ StringTokenizer st = new StringTokenizer(buffer, ", !’"); while(st.hasMoreTokens()){ String str=st.nextToken(); if(map.containsKey(str)) map.put(str, map.get(str)+1); else map.put(str, 1); } } Set<WordEntity> set=new TreeSet<WordEntity>(); for(String s:map.keySet()){ WordEntity wordEntry=new WordEntity(s,map.get(s)); set.add(wordEntry); } Iterator<WordEntity> ite=set.iterator(); int count=0; while(ite.hasNext()){ // String wr=ite.next(); if(count>=10) break; System.out.println(ite.next()); count++; } } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("檔案沒有找到"); } catch(IOException e){ System.out.println("檔案讀異常"); } } }

wordEntity類的實現:


public class WordEntity implements Comparable<WordEntity>{
private String word;
private Integer count;
public WordEntity(String key, Integer value) {
    super();
    this.word = key;
    this.count = value;
}
public String getKey() {
    return word;
}
public Integer getValue() {
    return count;
}
public void setKey(String key) {
    this.word = key;
}
public void setValue(Integer value) {
    this.count = value;
}
@Override
public String toString() {
    return "WordEntity key=" + word + ", 出現的次數" + count ;
}
@Override
public int compareTo(WordEntity O) {
    // TODO Auto-generated method stub
    int cmp=count.intValue()-O.count.intValue();
    return (cmp==0?word.compareTo(O.getKey()):-cmp);
}

}