后来想的写法,这样写比较简洁,用了些lambda表达式
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] input = sc.nextLine().split(" ");
        TreeMap<String, Set<String>> map = new TreeMap<>();
        String[] mark;
        Set<String> set;
        for (String s : input) {
            mark = s.split("-");
            set = !map.containsKey(mark[1]) ? new HashSet<>() : map.get(mark[1]);
            set.add(mark[0]);
            map.put(mark[1], set);
        }
        List<Map.Entry<String, Set<String>>> list = new ArrayList<>(map.entrySet());
        list.sort((Map.Entry<String, Set<String>> root, Map.Entry<String, Set<String>> next) ->
                next.getValue().size() - root.getValue().size() != 0 ? next.getValue().size() - root.getValue().size() : root.getKey().compareTo(next.getKey()));
        for (int i = 0; i < 3; i++) {
            System.out.println(list.get(i).getKey() + " " + list.get(i).getValue().size());
        }
    }
}