public class Demo {
public static void main(String[] args) {
String str="asaweqasdasdasdd";
char[]chars=str.toCharArray();
HashMap<Character,Integer> hs=new HashMap<Character,Integer>();
putInMap(hs,chars);
Set<Map.Entry<Character, Integer>>entrySet=hs.entrySet();
Iterator<Map.Entry<Character, Integer>>it=entrySet.iterator();
while(it.hasNext()){
Map.Entry<Character, Integer>entry=it.next();
System.out.println(entry.getKey()+"...."+entry.getValue());
}
}
private static void putInMap(HashMap<Character,Integer> hs, char[] chars) {
for(int i=0;i<chars.length;i++){
if(hs.get(chars[i])==null){
hs.put(chars[i], 1);
}else{
hs.put(chars[i], hs.get(chars[i])+1);
}
}
}
}
计算字符串中字符出现次数
|
|