我想实现对treemap关键字的倒排序,但排序后的结果是关键字排了,但值全是null了
public static Map sortMapByValue(Map map){
System.out.println("ok1");
// Map mapVK = new TreeMap();
Map mapVK = new TreeMap(new Comparator(){
public int compare(Object o1, Object o2) {
int v1 = ((Integer)o1).intValue();
int v2 = ((Integer)o2).intValue();
int s = v2 - v1;
if(s==0) return -1;
else return s;
}
});
System.out.println("ok2");
Collection col = map.keySet();
Vector v = new Vector(col);
//转置map
for(int i=0;i<v.size();i++){
String key = (String)v.get(i);
Integer value = (Integer)map.get(key);
System.out.println("ok3-" + i + "," +key);
mapVK.put(value,key);
System.out.println("ok>>>>" + (String)mapVK.get(value));
}
return mapVK;
}
public static Map sortMapByValue(Map map){
System.out.println("ok1");
// Map mapVK = new TreeMap();
Map mapVK = new TreeMap(new Comparator(){
public int compare(Object o1, Object o2) {
int v1 = ((Integer)o1).intValue();
int v2 = ((Integer)o2).intValue();
int s = v2 - v1;
if(s==0) return -1;
else return s;
}
});
System.out.println("ok2");
Collection col = map.keySet();
Vector v = new Vector(col);
//转置map
for(int i=0;i<v.size();i++){
String key = (String)v.get(i);
Integer value = (Integer)map.get(key);
System.out.println("ok3-" + i + "," +key);
mapVK.put(value,key);
System.out.println("ok>>>>" + (String)mapVK.get(value));
}
return mapVK;
}
解决方案 »
- 求java编程题目
- 程序中的@Override是什么意思?
- 为什么类成员方法只能访问类成员变量,而不能访问实例成员变量?
- Set中的元素是否重复,为什么是Iterator方法区分?
- 帮我编写一个含public String readLine()的程序
- Eclipse和JBUILDER各有什么优点和缺点?
- 北大的几道考题,急求问题答案。谢帮忙!!
- 救命啊!!!!!!
- 编译错误:javac 不是内部或外部命令,也不是可运行的程序或批处理文件
- 【求解】从数据库复制资料到数组中失败
- !!!!!!!!小弟初学JAVA 有一些小小问题,请高手用通俗语言能给我讲解一下吗????????????
- 希望大家给点学习java的意见!!!
你这样子写肯定取不到符合条件的数据if(s==0) return 0;