public static void main(String[] args) {
// 1、字符串
String str = "*Constructs a new <tt>HashMap</tt> with the same mappings as the * specified <tt>Map</tt>. The<tt>HashMap</tt> is created with default load factor (0.75) and aninitial capacity sufficient to*hold the mappings in the specified<tt>Map</tt>.";
// 2.把字符串转换为数组
char[] charArr = str.toCharArray();
// 3.创建一个Map
Map<Character, Integer> counterMap = new HashMap<Character, Integer>();
// 4.遍历一个Map
for (int i = 0; i < charArr.length; i++) {
// 5.拿到的字符作为键到集合中去找值
Integer value = counterMap.get(charArr[i]);
if (value == null) {
// 把字符作为键,1为值存入集合
counterMap.put(charArr[i], 1);
} else {
// 把值加1重新写入集合
value += 1;
counterMap.put(charArr[i], value);
}
}
Set<Map.Entry<Character, Integer>> entrySet = counterMap.entrySet();
for (Map.Entry<Character, Integer> entry : entrySet) {
System.out.println(entry.getKey() + " 字符出现次数=" + entry.getValue());
}
}