我有一個方法,需要一個單詞列表。這些單詞是針對以字符串作爲關鍵字,整數作爲值的單詞的hASHmap進行檢查的。字符串是一個單詞,整數表示文本文件中的單詞頻率。如何獲取重複?
目前,單詞列表按照頻率排列,並將它們放入Treemap中,頻率成爲關鍵。
但是,由於不可能有重複鍵,因此Hashmap中具有相同頻率值的任何單詞都不會輸入到Treemap中。
我能做些什麼才能使日期結構包含按其頻率排列的單詞(包括重複單詞)?
//given a list of words return a TreeMap of those words ranked by most frequent occurence
private TreeMap rankWords(LinkedList unrankedWords) {
//treemap to automatically sort words by there frequency, making the frequency count the key.
TreeMap<Integer, String> rankedWordsMap = new TreeMap<Integer, String>();
//for each of the words unranked, find that word in the freqMap and add to rankedWords
for (int i = 0; i < unrankedWords.size(); i++) {
if (freqMap.containsKey((String) unrankedWords.get(i))) {
rankedWordsMap.put(freqMap.get((String) unrankedWords.get(i)),
(String) unrankedWords.get(i));
}
}
return rankedWordsMap;
}
有**兩個''for-loops'並且如果找到了副本,則將其刪除。 – Tdorno 2013-05-06 17:35:22
@Tdorno但我想要包含具有重複頻率的詞 – user1835504 2013-05-06 17:36:46
使用'TreeMap <整數,列表>'。 –
Howard
2013-05-06 17:39:00