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 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())); }} 成绩排序_牛客题霸_牛客网 https://www.nowcoder.com/share/jump/5946710081711106665312