1.创建ArrayList存放100000个随机数
2.建立hashmap存放数字,
key重复 value+1
4.输出map值(重复的key和重复的次数)
重点:输出map内的键值对
for (Map.Entry<Integer,Integer> entry :map.entrySet())
public static void main(String[] args) {
HashMap<Integer,Integer> map = new HashMap<>();
ArrayList<Integer> list = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < 10_0000; i++) { //给list赋值
int num = random.nextInt(i);
list.add(num);
}
//遍历list
for (Integer key:list) {
if (map.get(key) == null){
map.put(key,1);
}else {
int val = map.get(key);
map.put(key,val+1);
}
}
for (Map.Entry<Integer,Integer> entry :map.entrySet()) {
if(entry.getValue() > 1) {
System.out.println("重复的数字:"
+entry.getKey()+"次数:"+entry.getValue());
}
}
}