题解 | #成绩排序# Stream流

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static Map map = new LinkedHashMap<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int num = Integer.parseInt(sc.nextLine());
         int index = Integer.parseInt(sc.nextLine());
        for(int i = 0; i < num; i++){
            String[] strs = sc.nextLine().split(&quot; &quot;);
            map.put(strs, Integer.parseInt(strs[1]));//用数组当key可以解决name重复的问题
        }
map.entrySet().stream().sorted((o1, o2) -> {
            if(index == 0){
                return o2.getValue() - o1.getValue();
            }else{
                return o1.getValue() - o2.getValue();
            }
}).forEach(o1 -> System.out.println(o1.getKey()[0] + &quot; &quot; + o1.getValue()));
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务