Map 的Key 或 Value 排序

来源:互联网 发布:非洲儿童举牌 知乎 编辑:程序博客网 时间:2024/05/17 06:00

Key:排序【正序】

TreeMap

Value:排序【正序】

Map<String,Integer> map = new HashMap<String,Integer>();

List<Map.Entry<String, Integer>> mapList = new ArrayList<Map.Entry<String,Integer>>(map.entrySet());

Collections.sort(mapList,new Comparator<Map.Entry<String, Integer>>(){
public int compare(Map.Entry<String, Integer> mp1, Map.Entry<String, Integer> mp2) {
return mp1.getValue() - mp2.getValue();
}
});

原创粉丝点击