public class MapTest01 {
public static void main(String[] args) {
//案例: 统计80个学生(单选景点)的意向
//1. 把80个学生选择的数据拿进来
String[] selects = {"A","B","C","D"};
StringBuilder sb = new StringBuilder();
Random r = new Random();
for (int i = 0; i < 80; i++) {
int n = r.nextInt(selects.length);
String m =selects[n];
sb.append(m);
}
//2. 定义一个Map集合记录最终统计的结果 : A=30 , B=20, C=20, D=10
HashMap<Character, Integer> infos = new HashMap<>();
//3.遍历80个学生选择的数据
for (int i = 0; i < sb.length(); i++) {
//4.提取当前选择景点字符
char ch = sb.charAt(i);
//5.判断Map集合中是否存在这个键
if (infos.containsKey(ch)){
infos.put(ch,infos.get(ch)+1);
}else{
// 说明此景点是第一次被选
infos.put(ch , 1);
}
}
System.out.println(infos);
}
}