private static void countcharecter(String str) {
char[] chars = str.toCharArray();
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (Character c : chars) {
if (Character.isWhitespace(c))
continue;
if (map.containsKey(c) == false) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
}
Set<Character> set = map.keySet();
Iterator<Character> it = set.iterator();
int count = 0;
Character cMax = ' ';
while (it.hasNext()) {
Character c = (Character) it.next();
System.out.println(c + ":" + map.get(c));
if (map.get(c) > count) {
cMax = c;
count = map.get(c);
}
}
System.out.println("最多出现的是:" + cMax + ":" + count);
}
找出字符串中出现次数最多的字符
最新推荐文章于 2023-11-21 08:00:00 发布