总共单词数:15
is:3
name:3
my:2
hello:1
said:1
what:1
Tom:1
your:1
John:1
he:1
直接上代码:
[plain]
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Map.Entry;
public class wordCount {
public static void main(String arg[]){
int wordCount=0; //用于统计单词的总个数
Map
String sentence="hello,my name is Tom,what is your name he said:\"my name is John\"";
while(token.hasMoreTokens()){ //循环遍历
wordCount++;
String word=token.nextToken(", .!:\"\"''\n"); //括号里的字符的含义是说按照,空格 . : "" '' \n去分割,如果这里你没明确要求,即括号里为空,则默认按照空格,制表符,新行符和回车符去分割
if(map.containsKey(word)){ //HashMap不允许重复的key,所以利用这个特性,去统计单词的个数
int count=map.get(word);
map.put(word, count+1); //如果HashMap已有这个单词,则设置它的数量加1
}
else
map.put(word, 1); //如果没有这个单词,则新填入,数量为1
}
System.out.println("总共单词数:"+wordCount);
sort(map); //调用排序的方法,排序并输出!
}
public static void sort(Map
List
Collections.sort(infoIds, new Comparator
public int compare(Map.Entry
return (o2.getValue() - o1.getValue());
}
}); //排序
for (int i = 0; i < infoIds.size(); i++) { //输出
Entry
System.out.println(id.getKey()+":"+id.getValue());
}
}
}
摘自 Bad Boy