2

The code below counts how many times the words and letters appeared in the string. How do I sort the output from highest to lowest? The output should be like:

the - 2
quick - 1 
brown - 1 
fox - 1
t - 2
h - 2
e - 2
b - 1

My code:

import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Tokenizer {
    public static void main(String[] args) {
        int index = 0;
        int tokenCount;
        int i = 0;
        Map<String, Integer> wordCount = new HashMap<String, Integer>();
        Map<Integer, Integer> letterCount = new HashMap<Integer, Integer>();
        String message = "The Quick brown fox the";

        StringTokenizer string = new StringTokenizer(message);

        tokenCount = string.countTokens();
        System.out.println("Number of tokens = " + tokenCount);
        while (string.hasMoreTokens()) {
            String word = string.nextToken().toLowerCase();
            Integer count = wordCount.get(word);
            Integer lettercount = letterCount.get(word);

            if (count == null) {
                // this means the word was encountered the first time
                wordCount.put(word, 1);
            } else {
                // word was already encountered we need to increment the count
                wordCount.put(word, count + 1);
            }
        }
        for (String words : wordCount.keySet()) {
            System.out.println("Word : " + words + " has count :" + wordCount.get(words));
        }
        for (i = 0; i < message.length(); i++) {
            char c = message.charAt(i);
            if (c != ' ') {
                int value = letterCount.getOrDefault((int) c, 0);
                letterCount.put((int) c, value + 1);
            }
        }

        for (int key : letterCount.keySet()) {
            System.out.println((char) key + ": " + letterCount.get(key));
        }
    }
}
James Dunn
  • 8,064
  • 13
  • 53
  • 87
Christian
  • 259
  • 3
  • 7
  • 21

3 Answers3

1

You have a Map<String, Integer>; I'd suggest something along the lines of another LinkedHashMap<String, Integer> which is populated by inserting keys that are sorted by value.

duffymo
  • 305,152
  • 44
  • 369
  • 561
1

It seems that you want to sort the Map by it's value (i.e., count). Here are some general solutions.

Specifically for your case, a simple solution might be:

  1. Use a TreeSet<Integer> to save all possible values of counts in the HashMap.

  2. Iterate the TreeSetfrom high to low.

  3. Inside the iteration mentioned in 2., use a loop to output all word-count pairs with count equals to current iterated count.

Please see if this may help.

Community
  • 1
  • 1
Tsung-Ting Kuo
  • 1,171
  • 6
  • 16
  • 21
1

just use the concept of the list and add all your data into list and then use sort method for it

  • 1
    This is not a decent answer. Please take the time to explain to the OP how they should approach this in detail. It's always good to write some code which demonstrates your answer. – APC Nov 17 '15 at 06:48