public static void main(String[] args) throws FileNotFoundException {
Map map = new HashMap<>();
map.put("张三", 100);
map.put("李四", 120);
map.put("王帅", 90);
map.put("赵小明", 70);
map.put("华仔", 100);
//Map转为Set
Set<Map.Entry<String,Integer>> entrySet = map.entrySet();
//创建List ,把Set放进去
List<Map.Entry<String,Integer>> list = new ArrayList(entrySet);
//排序
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
//从大到校 加个”-“
return -(o1.getValue()-o2.getValue());
}
});
for (Map.Entry<String, Integer> temp : list){
System.out.println(temp);
}
}
把学生的姓名和成绩录入到Map中,成绩从大到小输出
最新推荐文章于 2022-10-28 17:00:00 发布