Sorting a HashMap by Value
in
Programming Questions
•
1 year ago
Hello All,
I am attempting to sort a HashMap based on the Value. I want to use the most frequent words in a text. First I have to count them (which I can do), then I have to sort them by their number (which isn't working).
I've found several things that touch on it, and I think I'm very close, but it's not exactly working.
If you run the code you'll see that the 'sortHashMap' function is sorting them properly, but when I print the sortedMap, it's no longer sorted. Is there something with the .put function that I'm not realizing?
Thanks for looking.
p.s. - you'll also need some sort of text file.
- HashMap newWords; // HashMap object
- HashMap sortedWords;
- HashMap countedWords;
- String[] newTokens; // Array of all words from input file
- String[] commonTokens; // Array of all words from input file
- int counter;
- PFont displayFont;
- void setup(){
- size(400, 400);
- newWords = new HashMap();
- sortedWords = new HashMap();
- countedWords = new HashMap();
- // Load new file and chop it up
- String[] newLines = loadStrings("hamlet.txt");
- String allNewText = join(newLines, " ");
- //create hash table
- newTokens = splitTokens(allNewText, " -/%=#&,.?!:;[]1234567890()\t\n\r\f");
- //println(newTokens.length);
- for (int i=0; i<newTokens.length; i++){
- String newString = newTokens[i].toLowerCase();
- if (newWords.containsKey(newString)) {
- // Get the word object and increase the count
- // We access objects from a HashMap via its key, the String
- Word includedWord = (Word) newWords.get(newString);
- includedWord.countIncrease();
- } else {
- // Otherwise make a new word
- Word includedWord = new Word(newString);
- // And add to the HashMap
- // put() takes two arguments, "key" and "value"
- // The key for us is the String and the value is the Word object
- newWords.put(newString, includedWord);
- }
- }
- //print(newWords.keySet());
- // Make an iterator to look at all the things in the HashMap
- Iterator i = newWords.values().iterator();
- while (i.hasNext()) {
- Word wordChk = (Word) i.next();
- //println(wordChk.word + ": " + wordChk.count);
- countedWords.put(wordChk.word, wordChk.count);
- }
- sortedWords = sortHashMap(countedWords);
- //println(sortedWords);
- List sortKeys = new ArrayList(sortedWords.keySet());
- Iterator sortedI = sortKeys.iterator();
- while(sortedI.hasNext()){
- Object skey = sortedI.next();
- //println(skey.toString() + ": " + sortedWords.get(skey));
- }
- //println(sortedWords);
- }
- void draw(){
- }
- HashMap sortHashMap(HashMap input){
- List mapKeys = new ArrayList(input.keySet());
- List mapKeysClone = mapKeys;
- List mapValues = new ArrayList(input.values());
- List mapValuesClone = mapValues;
- //println(mapValues);
- Collections.sort(mapValues);
- //Collections.sort(mapKeys);
- //println(mapKeys);
- //println(mapValues);
- HashMap sortedMap = new HashMap();
- Iterator valueIt = mapValues.iterator();
- while (valueIt.hasNext()) {
- Object val = valueIt.next();
- Iterator keyIt = mapKeys.iterator();
- while (keyIt.hasNext()) {
- Object ky = keyIt.next();
- String comp1 = input.get(ky).toString();
- String comp2 = val.toString();
- if (comp1.equals(comp2)){
- input.remove(ky);
- mapKeys.remove(ky);
- println(ky.toString() + ": " + val);
- //println(val.getClass());
- sortedMap.put(ky.toString(), val);
- break;
- }
- }
- //println(sortedMap);
- }
- println(sortedMap);
- return sortedMap;
- }
- class Word {
- int count;
- String word;
- Word(String s) {
- word = s;
- count = 1;
- }
- void countIncrease() {
- count++;
- }
- }
1