public class Testing {
public static void main(String[] args) {
HashMap<String,Long> map = new HashMap<String,Long>();
ValueComparator bvc = new ValueComparator(map);
TreeMap<String,Long> sorted_map = new TreeMap<String,Long>(bvc);
map.put("A",99);
map.put("B",67);
map.put("C",67);
map.put("D",67);
System.out.println("unsorted map: "+map);
sorted_map.putAll(map);
System.out.println("results: "+sorted_map);
}
}
class ValueComparator implements Comparator<String> {
Map<String, Long> base;
//这里需要将要比较的map集合传进来
public ValueComparator(Map<String, Long> base) {
this.base = base;
}
public int compare(String a, String b) {
if (base.get(a) >= base.get(b)) {
return -1;
} else {
return 1;
}
}
}