package 小工具;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @author 无法手执玫瑰
* 2020/11/0030 17:16
*/
public class 统计各字符出现的次数 {
static void fun1(String s){
char[] chars = s.toCharArray();
//26个字母
int[] alphabetCount = new int[26];
for (char aChar : chars) {
alphabetCount[aChar - 'a']++;
}
String s1 = Arrays.toString(alphabetCount);
System.out.println(s1);
}
static void fun2(String s){
char[] chars = s.toCharArray();
Map<Character, Integer> map = new HashMap<>();
for (char aChar : chars) {
map.put(aChar,map.getOrDefault(aChar,0) + 1);
}
/*Set<Map.Entry<Character, Integer>> entries = map.entrySet();
entries.forEach(System.out::println);*/
for (Map.Entry<Character, Integer> entry: map.entrySet()){
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
public static void main(String[] args) {
String s = "aabbdcgs";
fun2(s);
}
}