public class Text {
public static void main(String[] args) {
String[] arr={"A","B","C","D"};
Random rd=new Random();
//统计投票状况
ArrayList<String> arrayList=new ArrayList<>();
for (int i = 0; i < 80; i++) {
arrayList.add(arr[rd.nextInt(arr.length)]);
}
HashMap<String, Integer> hm=new HashMap<>();
for (String name : arrayList) {
//检查是否存在
if (hm.containsKey(name)){
//有则更新数据
Integer count = hm.get(name);
count++;
hm.put(name,count);
}else {
//没有则添加并初始化
hm.put(name,1);
}
}
System.out.println(hm);
int max=0;
for (int i = 0; i <4 ; i++) {
Integer count = hm.get(arr[i]);
if (count>max){
max=count;
}
}
System.out.println(max);
for (int i = 0; i <4 ; i++) {
Integer count = hm.get(arr[i]);
if (count==max){
System.out.println(arr[i]);
}
}
}
}
统计投票人数 Map集合 java
最新推荐文章于 2024-07-13 13:55:40 发布