题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

import java.util.*;
import java.util.stream.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        sortStudent();
    }

    private static void sortStudent() {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        int sortType = in.nextInt();
        List<Map<String, Object>> rst = new ArrayList<>();
        while (num > 0 && in.hasNext()) {
            Map<String, Object> map = new HashMap<>();
            String name = in.next();
            int socre = in.nextInt();
            map.put("name", name);
            map.put("socre", socre);
            rst.add(map);
            num--;
        }

        if (sortType == 0) {
            rst = rst.stream().sorted(Comparator.comparingInt(map -> (int) ((
                                          Map<String, Object>) map).get("socre")).reversed()).collect(
                      Collectors.toList());
        } else if (sortType == 1) {
            rst = rst.stream().sorted(Comparator.comparingInt(map -> (int) ((
                                          Map<String, Object>) map).get("socre"))).collect(
                      Collectors.toList());
        }
        rst.forEach(map -> {
            System.out.println(map.get("name") + " " + map.get("socre"));
        });
    }

}

全部评论

相关推荐

11-04 19:05
已编辑
东莞城市学院 单片机
不知道怎么取名字_:你这个要实习两年?哪有这么久的,感觉就是即使你毕业了,但还按实习的话,是不是不用给你缴社保公积金啥的
点赞 评论 收藏
分享
11-03 12:40
中山大学 Java
勇敢的突尼斯海怪选钝...:楼主这拒意向话术好得体呀 !求问HR回复态度咋样呀
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务