到目前爲止,我已經能夠顯示每個單詞的出現次數,但是如何根據出現次數對單詞進行排序?如何根據問題的數量對單詞進行排序?
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());
}
}
你是什麼'分裂( 「[\\ S + \\ p {P}]」 )'假設分裂? – Pshemo