public static void main(String[] args) {
String str = "yekmaakkccekymbvb";
Map<String,Integer> map=new HashMap<String,Integer>();
//循环取值放入map中
for (int i = 0; i < str.length(); i++) {
Integer value=1;
if(map.containsKey(str.substring(i, i + 1)))//当已存在值时加1
{
value=map.get(str.substring(i, i + 1));
value=value+1;
}
map.put(str.substring(i, i + 1),value);
}
System.out.println("字符长度:"+str.length());
//根据KEY值遍历
for(String key:map.keySet())
{
System.out.println("字符:"+key+",次数:"+map.get(key));
}
} |