Java8: Create HashMap with character count of a String

前端 未结 4 1093
情深已故
情深已故 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-06 23:55

         String str = "Hello Manash";
        Map hm = str.chars().mapToObj(c-> 
        (char)c).collect(Collectors.groupingBy(c->c,Collectors.counting()));
        System.out.println(hm);
    

提交回复
热议问题