想知道是否有比计算给定字符串的字符数更简单的方法,如下所示?
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);发布于 2020-01-27 17:55:08
String str = "Hello Manash";
Map<Character,Long> hm = str.chars().mapToObj(c->
(char)c).collect(Collectors.groupingBy(c->c,Collectors.counting()));
System.out.println(hm);https://stackoverflow.com/questions/55129028
复制相似问题