• 有10万个数据,数据范围是[0,1000),统计每个数据出现的次数


          每个数据都是随机产生的,Random函数,考虑到要统计每个数字出现的次数,用hashmap,用key来统计出现的数字(key不可以重复),用value来统计出现的次数。

    关键代码:

    import java.util.*;
    public class StringDemo {
        public static void main(String[] args) {
            Map<Integer, Integer> map = new HashMap<>();
            Random random = new Random();
            for (int i = 0; i < 100000; i++) {
                Integer b = random.nextInt(1000);
                int count = 1;
                int count1 = 0;
                if (map.containsKey(b)){
                    count1 = map.get(b);//获取已经出现的个数
                    count1++;
                    map.put(b,count1);
                }else {
                    //第一次出现
                    map.put(b,count);
                }
            }
    
            Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<Integer,Integer> next = iterator.next();
                Integer key = next.getKey();
                Integer value = next.getValue();
                System.out.println(key+"出现次数:"+value+"  ");
            }
        }
    }
  • 相关阅读:
    java并发初探CountDownLatch
    java并发LockSupport
    java并发初探ReentrantWriteReadLock
    mysql视图初探
    mysql索引
    java并发AtomicIntegerFieldUpdater
    php7.* 新特性
    搜索引擎 对比
    2021-03-09 吐槽
    linux 进程&线程
  • 原文地址:https://www.cnblogs.com/128-cdy/p/12284824.html
Copyright © 2020-2023  润新知