题解HJ68 | #成绩排序#

成绩排序

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

import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static Map<String[], Integer> map = new LinkedHashMap<>();
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = Integer.parseInt(in.nextLine());
        int asc = Integer.parseInt(in.nextLine());

        for (int i = 0; i < count; i++) {
            String[] strs = in.nextLine().split(" ");
            map.put(strs, Integer.parseInt(strs[1]));
        }

        map.entrySet().stream().sorted(((o1, o2) -> {
            if (asc == 1) return o1.getValue() - o2.getValue();
            else return o2.getValue() - o1.getValue();
        })).forEach(x -> System.out.println(x.getKey()[0] + " " + x.getValue()));
    }
}

stream自定义排序的简单应用 名字可能会重复所以在输入时选择了数组(虽说这一点我也没看懂)

全部评论

相关推荐

Rac000n:淘天-客户运营部-AI研发工程师,智能客服方向,暑期实习招聘,欢迎联系
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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