public static void main(String[] args) {
String str = "fsdgdfxsa4547456ersc 43563fds sdagfg";
//新建一个HashMap对象;
Map<Character, Integer> map = new HashMap<>();
//通过for循环,把String的键值存放到map
for (int i = 0; i < str.length(); i++) {
//通过循环,找到字符串的每一位字符并存入到temp中;
char temp = str.charAt(i);
//如果map里面有temp这个字符,把temp的值加1
if (map.containsKey(temp)){
map.put(temp,map.get(temp)+1);
}else {
//如果map里面没有temp这个字符,把temp的值设为1
map.put(temp,1);
}
}
//调用Collections类的max方法,获取map的值的集合;并找出最大的那个值;
int maxNum = Collections.max(map.values());
//建立一个set对象
Set<Character> set = new HashSet<Character>();
//通过集合的循环,把map的值放到entry1里,通过entry1找到值最大的maxNum的key;
for (Map.Entry<Character,Integer> entry : map.entrySet()){
if (entry.getValue() == maxNum){
set.add(entry.getKey());
}
}
System.out.println("出现次数最多的字母为:" + set + " 最多出现次数为" + maxNum);
}