2015-11-12 53 views
1

到目前爲止,我已經能夠顯示每個單詞的出現次數,但是如何根據出現次數對單詞進行排序?如何根據問題的數量對單詞進行排序?

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

    } 

} 
+0

你是什麼'分裂( 「[\\ S + \\ p {P}]」 )'假設分裂? – Pshemo

回答

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)); 
} 
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; 
} 
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); 
      } 
     } 
相關問題