import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.swing.text.html.HTMLDocument.Iterator;
public class CountChar {
//有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数。
public static void main(String[] args) {
//定义字符串
String str="我a不zhi道不hui做123456";
HashMap hm=new HashMap();
for(int i=0;i<str.length();i++){
char c=str.charAt(i);
Integer num=(Integer) hm.get(c);
if(num==null){
num=1;
}
else
num++;
hm.put(c, num);
}
System.out.println(hm);
}
}
转载于:https://blog.51cto.com/913061291/1301123