
字符串处理问题,思路:
把字符串分割成单独的字符,再把所有独立字符存到TreeMap中,这样既能维护每个字符出现的次数,又能把字符排序,方便最后输出
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class CharStatistics {
public static void main(String[] args) {
//数据输入
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
sc.close();
//数据处理
Map<Character,Integer> map = new TreeMap<Character,Integer>(); //统计每个字符的个数,按自然排序
int n = s.length();
for(int i=0 ; i<n ; i++) {
char key=s.charAt(i);
if( map.containsKey(key) ) {
map.put(key,map.get(key)+1);
}else {
map.put(s.charAt(i), 1);
}
}
int max=0;
for (Map.Entry<Character,Integer> e : map.entrySet() ) { //找出最多次数
max=Math.max(max,e.getValue());
}
//数据输出
for(Map.Entry<Character,Integer> e : map.entrySet()) { //根据最多次数打印所有符合字符
if(max==e.getValue())
System.out.print(e.getKey());
}
}
}