Java8: Create HashMap with character count of a String

前端 未结 4 1094
情深已故
情深已故 2021-01-06 23:33

Wondering is there more simple way than computing the character count of a given string as below?

String word = \"AAABBB\";
    Map ch         


        
4条回答
  •  一向
    一向 (楼主)
    2021-01-07 00:08

    Figured out, below is another simple way.

    Map charCount = new HashMap();
        for(String charr: s.split("")){
            charCount.put(charr,charCount.getOrDefault(charr,0)+1);
    }
    

提交回复
热议问题