如何使用HashMap集合统计字符串中某个字符出现的次数?

 还有比下面这个计算给定字符串的字符数更简单的方法

 

String word = "AAABBB";
    Map<String, Integer> charCount = new HashMap();
    for(String charr: word.split("")){
        Integer added = charCount.putIfAbsent(charr, 1);
        if(added != null)
            charCount.computeIfPresent(charr,(k,v) -> v+1);
    }

    System.out.println(charCount);

 

String word = "AAA BBB CCC AAA BBB";
Map<String, Long> map = Arrays.stream(word.split(" ")).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(map);