什么是排序大量字词列表(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