I have a method that takes in a list of words. These words are checked against a hASHmap of words that has a String as a key, and an Integer as a value. The String is a word, and the Integer represents that words frequency in a text file.
Currently the list of words are ranked according to their frequency by putting them into a Treemap with the frequency becoming the key.
However, as there can be no duplicate keys, any words with the same frequency value in the Hashmap will not be entered into the Treemap.
What could I do in order to have a date structure that contains the words ranked by their frequency including duplicates?
//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;
}
You should re-think your data structure in order to have unique keys. It sounds like your structure is inverted: it should be a Map of words to counts, not the other way around, as the words are the unique key, and the counts are the value data associated with the keys.
I would start with a Map of String to Integer frequency.
Copy the entrySet() to a List and sort it by frequency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With