泛型方法 public static <T> void display(T t)
public class Test01 implements Comparator<Long>{
Map<Long,Double> base;
public Test01(Map<Long,Double> base){
this.base = base;
}
@Override
public int compare(Long a,Long b) {
if(base.get(a).doubleValue() >= base.get(b).doubleValue()){
return -1;
}else{
return 1;
}
}
//泛型方法 public static <T> void display(T t)
public static <K,V extends Comparable<V>> Map<K,V> sortByValues(final Map<K,V> map){
Comparator<K> valueComparator = new Comparator<K>(){
public int compare(K k1,K k2){
int compare = map.get(k2).compareTo(map.get(k1));
if(compare == 0)
return 1;
else
return compare;
}
};
Map<K,V> sortedByValues = new TreeMap<K,V>(valueComparator);
sortedByValues.putAll(map);
return sortedByValues;
}
public static void main(String args[]){
HashMap<Long,Double> map = new HashMap<Long,Double>();
map.put(1L, 99.5);
map.put(2L, 67.2);
map.put(3L, 67.5);
map.put(4L, 67.6);
Test01 bvc = new Test01(map);
TreeMap<Long,Double> sorted_map = new TreeMap<Long,Double>(bvc);
System.out.println("unsorted map:"+map);
sorted_map.putAll(map);
System.out.println("results:" + sorted_map);
Map<Long,Double> sorted_map2 = sortByValues(map);
System.out.println("results2:" + sorted_map2);
}
}
转载于:https://blog.51cto.com/xiatianyangwang/1425410