- /**统计字符串中每个字符出现的次数 String str = "aaaabbbcccccccccc";*/
- String str = "aaaabbbcccccsdsdeeeccccc";
- char[] arr = str.toCharArray();
- HashMap<Character, Integer> hm = new HashMap<>();
- for(char c:arr){
- //同等上列
- hm.put(c, !hm.containsKey(c)?1:hm.get(c)+1);
- }
- for(char c:hm.keySet()){
- System.out.print(c+"("+hm.get(c)+")");
- }
复制代码 |
|