方法很low,希望大家给出修改方向
import java.util.*; public class Main{ public static void main(String [] args){ getCharacterSort(); } public static void getCharacterSort(){ Scanner scan = new Scanner(System.in); while(scan.hasNext()){ String input = scan.nextLine(); Map<String, Integer> map = new HashMap<>(); int len = input.length(); for(int i = 0 ; i < len; i++){ String temp = input.charAt(i) + ""; if(map.containsKey(temp)){ map.put(temp, map.get(temp) + 1); } else{ map.put(temp, 1); } } // 针对map的value进行排序 List<Integer> list = new ArrayList(map.values()); Integer[] val = list.toArray(new Integer[list.size()]); Arrays.sort(val); for(int i = val.length-1; i >= 0; i--){ List<String> temp = new ArrayList<>(); for(String key: map.keySet()){ if(val[i] == map.get(key)){ temp.add(key); } } int size = temp.size(); i = i - size + 1; if(size > 1){ String [] keys = temp.toArray(new String[size]); Arrays.sort(keys); for(int j = 0; j < keys.length; j++){ System.out.print(keys[j]); } } else { System.out.print(temp.get(0)); } } System.out.println(); } } }