有一个字符串String str = "sdfsadfgasftegreghwetghwethtehwerhger"
1. 统计出每个字符出现的次数
import java.util.HashMap;
import java.util.Map;
public class Num {
public static void main(String[] args) {
String str = "sdfsadfgasftegreghwetghwethtehwerhger";
Map<String,Integer>map=new HashMap<>();
char[]c=str.toCharArray();
for (int i = 0; i < c.length; i++) {
char ch=c[i];
if(map.get(String.valueOf(ch))==null){
map.put(String.valueOf(ch),1);
}else {
map.put(String.valueOf(ch), map.get(String.valueOf(ch))+1);
}
}
System.out.println(map);
}
}
运行结果: