面试题描述:
统计一个数组中,重复字符出现的次数,并以关联数组的形式输出。
String[] arr = {"D","B","A","B","C","A","A","B","D","C","D","D"};
Map<String, Long> r =
Stream.of(arr).collect(
Collectors.groupingBy(Function.identity(), Collectors.counting())
);
System.out.println(r);
![tangcheng](https://i-blog.csdnimg.cn/blog_migrate/066bf16f06b3b4050622378b7e73cb40.jpeg)
![tangcheng](https://i-blog.csdnimg.cn/blog_migrate/227133f4ab69f1a547e95901fb5de248.jpeg)
如果需要找出出现次数最多的字符串,可以使用如下代码:
![](https://i-blog.csdnimg.cn/blog_migrate/049feea07b42b846c15df0cda3449af9.jpeg)