1

So far I have been able to display the number of occurrence of each word but how can I sort the words by the number of occurrences?

import java.util.*;

public class CountOccurrenceOfWords {
  public static void main(String[] args) {
    String text = "Hello, I am a working class citizen who is independent and driven to be the best that I can be";

    Map<String, Integer> map = new TreeMap<>();
    String[] words = text.split("[\\s+\\p{P}]");
    for (int i = 0; i < words.length; i++) {
      String key = words[i].toLowerCase();

      if (key.length() > 0) {
        if (!map.containsKey(key)) {
          map.put(key, 1);
        }
        else {
          int value = map.get(key);
          value++;
          map.put(key, value);
        }
      }
    }

    Set<Map.Entry<String, Integer>> entrySet = map.entrySet();

    for(Map.Entry<String, Integer> entry: entrySet)
      System.out.println(entry.getKey() + "\t" + entry.getValue());  

   }

}
void
  • 7,760
  • 3
  • 25
  • 43
Karan K
  • 21
  • 7

3 Answers3

2

You could get what you want by using a comparator.


For example:

static Comparator<String> DescendingFrequencyComparator = new Comparator<String>() {
    public int compare(String s1, String s2) {
        return map.get(s2).compareTo(map.get(s1));
    }
};

Then use it as:

ArrayList<String> allWords = new ArrayList<String>(map.keySet());
allWords.sort(DescendingFrequencyComparator);
for(String s: allWords) {
    System.out.println(s + "\t" + map.get(s));
}
Roney Michael
  • 3,964
  • 5
  • 30
  • 45
0

something like

public LinkedHashMap sortHashMapByValues(HashMap passedMap) {
   List mapKeys = new ArrayList(passedMap.keySet());
   List mapValues = new ArrayList(passedMap.values());
   Collections.sort(mapValues);
   Collections.sort(mapKeys);

   LinkedHashMap sortedMap = new LinkedHashMap();

   Iterator valueIt = mapValues.iterator();
   while (valueIt.hasNext()) {
       Object val = valueIt.next();
       Iterator keyIt = mapKeys.iterator();

       while (keyIt.hasNext()) {
           Object key = keyIt.next();
           String comp1 = passedMap.get(key).toString();
           String comp2 = val.toString();

           if (comp1.equals(comp2)){
               passedMap.remove(key);
               mapKeys.remove(key);
               sortedMap.put((String)key, (Double)val);
               break;
           }

       }

   }
   return sortedMap;
}
AbtPst
  • 7,778
  • 17
  • 91
  • 172
0
public static void main(String[] args) {
        ArrayList<Integer> wordsList = new ArrayList<>();
        String text = "Hello, I am a working class citizen who is independent and driven to be the best that I can be";

        Map<String, Integer> map = new TreeMap<>();
        String[] words = text.split("[\\s+\\p{P}]");
        for (int i = 0; i < words.length; i++) {
            String key = words[i].toLowerCase();

            if (key.length() > 0) {
                if (!map.containsKey(key)) {
                    map.put(key, 1);
                } else {
                    int value = map.get(key);
                    value++;
                    map.put(key, value);
                }
            }
        }

        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();

        for (Map.Entry<String, Integer> entry : entrySet){
            System.out.println(entry.getKey() + "\t" + entry.getValue());
            for (int i=0; i<entrySet.size(); i++){

                wordsList.add(entry.getValue());
            }
           for(Integer counter : wordsList){
               System.out.println(counter);
           }
        }
Michael Queue
  • 1,340
  • 3
  • 23
  • 38