Java中如何实现Map的按值Value(非Key)排序

Map map = new TreeMap();

map.put("j2se", 20);
map.put("j2ee", 10);
map.put("j2me", 30);

List> infoIds = new ArrayList>(
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>() {
public int compare(Map.Entry o1,
Map.Entry 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 

你可能感兴趣的:(Java中如何实现Map的按值Value(非Key)排序)