1 public class ADemo {
2 public static void main(String[] args) {
3 Scanner sc = new Scanner(System.in);
4 System.out.println("请输入字符串");
5 String str = sc.next();
6 toMap(str);
7
8
9 }
10 public static void toMap(String str){
11 Map<Character,Integer> m = new HashMap<>();
12 char ch;
13 for (int i = 0; i < str.length(); i++) {
14 ch = str.charAt(i);
15 if(m.keySet().contains(ch)){
16 m.put(ch,m.get(ch)+1); //可以覆盖原来的值。
17 }else{
18 m.put(ch,1);
19 }
20 }
21 for (Character key : m.keySet()) {
22 System.out.println(key+"出现的次数:"+m.get(key));
23 }
24 }