劝退写法:利用map存储字符以及次数,再用Collections.sort()进行排序,排序需要把map转换成list。

public class Main{
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
           String str = sc.nextLine();
           Map<Character,Integer> map = new HashMap<>();
            for(int i = 0 ;i<str.length();++i){
               if(map.containsKey(str.charAt(i))){
                   map.put(str.charAt(i),map.get(str.charAt(i))+1);
               }
               else {
                   map.put(str.charAt(i),1);
               }
           }
            List<Map.Entry<Character,Integer>> list = new ArrayList<>(map.entrySet());
            Collections.sort(list,new Comparator<Map.Entry<Character,Integer>>(){
                public  int compare(Map.Entry<Character,Integer> k1,Map.Entry<Character,Integer> k2){
                    if(k1.getValue()==k2.getValue()){
                        return k1.getKey()-k2.getKey();
                    }else return k2.getValue()-k1.getValue();
                }
            });
            for(Map.Entry<Character,Integer> entry:list){
                System.out.print(entry.getKey());
            }
             System.out.println();
        }
        
    }
}