您的位置:首页 > 产品设计 > UI/UE

Map中的TreeMap根据value排序

2017-08-16 17:16 375 查看
需要借助于Collections的sort(List list,Comparator

public static void main(String[] args) {
Map<String, Integer> map=new TreeMap<>();
map.put("A", 30);
map.put("B", 90);
map.put("C", 80);
map.put("D", 65);
map.put("E", 89);
map.put("F", 75);
//对value进行排序
//将map.entrySet()转换为list集合
List<Map.Entry<String, Integer>> list= new ArrayList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {

@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
// TODO Auto-generated method stub
return o1.getValue().compareTo(o2.getValue());
}
});
for (Entry<String, Integer> entry : list) {
System.out.println(entry.getKey()+"="+entry.getValue());
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: