按TreeMap中的值进行排序,而不是键,代码出错了~~

fp862 2011-12-09 01:26:20
http://topic.csdn.net/u/20110621/18/db4e6e64-da20-4933-8fa3-3b5a870d71ff.html?seed=35017025&r=76858240#r_76858240
首先,请看上面链接中的帖子。


package datastruct;

import java.util.*;
import java.util.Map.*;
public class CountOccurrenceOfWords1 {
public static void main(String[] args){
String text = "Having a good day. Have a good class." +
"Having a good visit. Have fun!";

Map<String,Integer> hashMap = new HashMap<String,Integer>();

String[] words = text.split("[ .!?]");
for(int i=0;i<words.length;++i){
if(words[i].length()>1){
if(hashMap.get(words[i]) != null){
int value = hashMap.get(words[i]).intValue();
value ++;
hashMap.put(words[i],value);
}else{
hashMap.put(words[i],1);
}
}
}
Map<String,Integer> treeMap = new TreeMap<String,Integer> (new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o2.getValue() - o1.getValue());
}
});
treeMap.putAll(hashMap);
System.out.println("Display words and their count in"+" ascending order of the words1");
System.out.println(treeMap);
}
}


上面的代码出错了,我还不知道怎么改,请知道的赐教了。
...全文
46 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧