public class Frequency {
public static void main(String[] args) {
Map<String, Integer> m = new TreeMap<String, Integer>();// TreeMap...带排序的Map
String str = "中国,美国,日本,朝鲜,中国,中国,美国,越南";
String[] strArray = str.split(",");
for (String word : strArray) {
Integer freq = m.get(word);// 获取此单词以前出现的次数
m.put(word, (freq == null ? 1 : freq + 1));
}
//System.out.println(m);
Set<String> set = m.keySet();
for (String string : set) {
System.out.println(string + "出现的次数:" + m.get(string));
}
}
}
统计单词出现的次数
最新推荐文章于 2020-02-25 10:14:24 发布