使用Map判断一个字符串中单个字符出现的次数

String str="heqoikjzxdlawskjhelgeq";

Map<Charactre,Integer> map=new HashMap();



for(int i=0;i<str.length();i++){

    char c=str.charAt(i)

    }

if(map.containsKey(c)){

    map.put(c,map.get(c)+1);

    //get(key)返回的就是value值,就是出现的次数

}else{
    map.put(c,1)
    }

for(Map.Entry<Character,Integer> entry:map.entrySet()){

    System.out.println(entry.getKey() + ":"+ entry.getValue());

     }

猜你喜欢

转载自blog.csdn.net/qq_40531768/article/details/89453999