1、取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)...
String line = "abcdekka27qoq";
char[] arr = line.toCharArray();
HashMap<Character, Integer> hm = new HashMap<>();
for (char c : arr) {
hm.put(c, hm.containsKey(c) ? hm.get(c) + 1 : 1);
}
for (Character key : hm.keySet()){
System.out.print(key + "(" +hm.get(key) + ")");
} |
|