什麼是排序大量字詞列表(10,000-20,000)的最佳/最簡單的方式是按列表中出現的次數(Java)排序。我嘗試了一個基本的實現,但我得到了一個內存不足的運行時錯誤,所以我需要一個更有效的方法。你會建議什麼?最簡單的方式來按字號排序字詞列表
ArrayList<String> occuringWords = new ArrayList<String>();
ArrayList<Integer> numberOccur = new ArrayList<Integer>();
String temp;
int count;
for(int i = 0; i < finalWords.size(); i++){
temp = finalWords.get(i);
count = 0;
for(int j = 0; j < finalWords.size(); j++){
if(temp.equals(finalWords.get(j))){
count++;
finalWords.remove(j);
j--;
}
}
if(numberOccur.size() == 0){
numberOccur.add(count);
occuringWords.add(temp);
}else{
for(int j = 0; j < numberOccur.size(); j++){
if(count>numberOccur.get(j)){
numberOccur.add(j, count);
occuringWords.add(j, temp);
}
}
}
}
其中,finalWords是所有字符串的列表。我必須將每個單詞出現的次數存儲在單獨的數組列表中,因爲我想不出讓每個單詞成爲單獨對象的更好方法。
C#LINQ將使它沒有道理的!請參閱http://stackoverflow.com/questions/454601/how-to-count-duplicates-in-list-with-linq 它使用弗拉德的算法。雖然,不是hashmap。 – Fakrudeen 2010-03-03 06:42:11