按照字母的出现频次进行排序,大小写敏感
public static void main(String[] args) {
//Main main = new Main();
Scanner input = new Scanner(System.in);
String s = input.nextLine();
HashMap <Character,Integer> table = new HashMap<>();
for(int i = 0; i < s.length();i++)
{
if(table.containsKey(s.charAt(i)))
{
int nums = table.get(s.charAt(i));
nums++;
table.put(s.charAt(i),nums);
}
else
{
table.put(s.charAt(i),1);
}
}
TreeMap<Character,Integer> t = new TreeMap<>(table);//进行排序
for(Character c: t.keySet())
{
for(int i = 0; i < t.get(c);i++)
System.out.print(c);//有多少个输出多少个
}
}
举例子说明