如何根据骚扰的数量对单词进行排序?

时间:2015-11-12 19:39:42

标签: java

到目前为止,我已经能够显示每个单词的出现次数,但是如何根据出现次数对单词进行排序?

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());  

   }

}

3 个答案:

答案 0 :(得分:2)

你可以使用比较器得到你想要的东西。

例如:

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

然后将其用作:

ArrayList<String> allWords = new ArrayList<String>(map.keySet());
allWords.sort(DescendingFrequencyComparator);
for(String s: allWords) {
    System.out.println(s + "\t" + map.get(s));
}

答案 1 :(得分:0)

类似

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;
}

答案 2 :(得分: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);
           }
        }