Java如何对HashMap按值进行排序

时间:2022-10-13 19:14:52

HashMap的值是没有顺序的,它是按照key的HashCode来实现的。对于这个无序的HashMap我们要怎么来实现排序呢?(TreeMap类似)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


Map<String, Long> map = new HashMap<String, Long>();
map.put("c", 33333L);
map.put("a", 11111L);
map.put("d", 44444L);
map.put("e", 55555L);
map.put("b", 22222L);

//将map.entrySet()转换成list
List<Map.Entry<String, Long>> list = new ArrayList<Map.Entry<String, Long>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Long>>() {
//降序排序
@Override
public int compare(Entry<String, Long> o1, Entry<String, Long> o2) {
//return o1.getValue().compareTo(o2.getValue());
return o2.getValue().compareTo(o1.getValue());
}
});

for (Map.Entry<String, Long> mapping : list) {
System.out.println(mapping.getKey() + ":" + mapping.getValue());
}


运行结果:

e:55555d:44444c:33333b:22222a:11111