用TreeMap可解决排序问题,键为字符串,值为字符串的个数。

import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;


public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Map<String,Integer> map = new TreeMap<String,Integer>();
        int n = scanner.nextInt();
        for(int i = 0;i < n;i ++) {
            String s = scanner.next();
            map.put(s,map.getOrDefault(s, 0) + 1);
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            for(int i = 0;i < entry.getValue();i ++) {
                System.out.println(entry.getKey());
            }
        }
        scanner.close();
    }
}