Map<String, Integer> map = new TreeMap<String, Integer>();
map.put("j2se", 20);
map.put("j2ee", 10);
map.put("j2me", 30);
List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(
map.entrySet());
//排序前
for (int i = 0; i < infoIds.size(); i++) {
String id = infoIds.get(i).toString();
System.out.println(id);
}
//排序
Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return (o2.getValue() - o1.getValue());
}
});
//排序后
for (int i = 0; i < infoIds.size(); i++) {
String id = infoIds.get(i).toString();
System.out.println(id);
}
排序前:
j2ee=10
j2me=30
j2se=20
排序后:
j2me=30
j2se=20
j2ee=10
3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/