final String str = "a aa, aa aaa aaa. aaaa aaaa aaaa aaaa aaaa ";
Map map = new HashMap();
StringTokenizer token = new StringTokenizer(str.toUpperCase(), ",. ");
int count = 1;
while (token.hasMoreElements()) {
String str2 = token.nextToken();
if(map.containsKey(str2)){
map.put(str2,count++);
}else{
map.put(str2,1);
}
}
Set set = map.keySet();
for (Iterator iter = set.iterator(); iter.hasNext();) {
String key = (String) iter.next();
String num = (String) map.get(key).toString();
System.out.println(key + ":" + num);
}
输出的结果是:AAAA:6
AA:1
AAA:2
A:1
如果还需排序该怎么办?
正确的结果是:AAAA:5
AA:2
AAA:2
A:1 |