import java.util.HashMap;
public class TheSameSum {
public static void count(String str){
char[] chars = str.toCharArray();
HashMap<Character,Integer> hm = new HashMap();
for(char c : chars){
if(!hm.containsKey©){
hm.put(c,1);
}else{
hm.put(c, hm.get©+1);
}
}
for(Character key: hm.keySet()){
System.out.println(key + "====" + hm.get(key));
}
}
public static void main(String[] args) {
String str = "abcaaaefdabbhg";
count(str);
}
}
该博客展示了如何使用Java实现一个字符统计的方法,通过创建HashMap存储字符串中每个字符及其出现次数。代码中,将字符串转换为字符数组,遍历并更新哈希映射,最后打印每个字符及其出现频率。这种方法对于理解和应用哈希映射数据结构非常有帮助。

被折叠的 条评论
为什么被折叠?



