当前页面: 开发资料首页 → J2SE 专题 → 帮我看下这段代码 关于treemap排序的
帮我看下这段代码 关于treemap排序的
摘要: 帮我看下这段代码 关于treemap排序的
我想实现对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
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;
}
up
mapVK.put(value,key);没有PUT进去
if(s==0) return -1;//????相同为啥取反,get的时候也要compare的。
你这样子写肯定取不到符合条件的数据
if(s==0) return 0;