使用Map对字符串中的字符计数
代码如下:
/**
* 使用Map的key不重复的特性
*/
public class Test {
public static void main(String[] args) {
// 测试数据
String str = "abcd1234eeefffasdbouhnllzxxxehyaweghxx7235650egepiikmngyuijbgwe";
// 用于计数的Map
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
map.put(c, map.get(c) == null ? 1 : map.get(c) + 1);
}
System.out.println(map);
}
}