自己看下代码吧
public static void main(String[] args) {
final HashMap<String, Integer> hm = new HashMap<>();
hm.put("张三", 23);
hm.put("李四", 21);
hm.put("王五", 26);
hm.put("赵六", 14);
TreeSet<String> ts = new TreeSet<>(new Comparator<String>() {
public int compare(String s1, String s2) {
int num = hm.get(s1) - hm.get(s2);
return num == 0 ? 1 : num;
}
});
ts.addAll(hm.keySet());
LinkedHashMap<String, Integer> lhm = new LinkedHashMap<>();
for(String key : ts) {
lhm.put(key, hm.get(key));
}
System.out.println(lhm);
} |