import java.util.*;
public class MyMap {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeMap map = new TreeMap(new MyComparator());
map.put("1", "AA");
map.put("3", "CC");
map.put("2", "BB");
map.put("5", "EE");
map.put("4", "DD");
map.put("6", "FF");
map.put("8", "HH");
map.put("7", "GG");
map.put("9", "II");
//我只输入了9个值,因为我输入15个值,
他就不好好的排序了。不懂why
Set keySet=map.keySet();
Iterator it =keySet.iterator();
while(it.hasNext()) {
Object key =it.next();
Object value=map.get(key);
System.out.println(key+":"+value);
}
}
}
class MyComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
String ele1 = (String) obj1;
String ele2 = (String) obj2;
return ele2.compareTo(ele1);
}
}
Iterator it =keySet.iterator();
while(it.hasNext()) {
Object key =it.next();
Object value=map.get(key);
System.out.println(key+":"+value);
}
}
}
class MyComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
String ele1 = (String) obj1;
String ele2 = (String) obj2;
return ele2.compareTo(ele1);
}
}
结果: