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

fp862 fp862 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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);
}
}


上面的代码出错了,我还不知道怎么改,请知道的赐教了。
...全文
评论
收藏 已收藏
分享
返回顶部
fp862
Java SE 62,621 成员 307,251 内容