却诚Salong 2021-04-01 13:53 采纳率: 83.3%
浏览 552
已结题

关于hashMap和LinkedHashMap的查询速度问题

网上看了很多博客,关于hashMap和LinkHashMap的区别时候,基本上都说的是HashMap查询速度快,LinkedHashMap的查询速度慢,我自己测试了一下,代码如下:

结果不管我设置一次插入数值多少,写入的速度和读取的速度,都是LinkedHashMap吊打hashMap,有大佬来解释一下原因么?

public static void insertHashMapTest() throws InterruptedException {
        int size=50000;
        HashMap<String, Double> hashMap = new HashMap<>(size);
        System.out.println("----开始测试hashMap---");
        long hashMapStart = System.currentTimeMillis();
        for(int i=0;i<size;i++){
            hashMap.put(UUID.randomUUID().toString(),Math.random());
        }
        System.out.println("hashMap插入"+size+"条数据消耗时间为:"+(System.currentTimeMillis()-hashMapStart));
        long hashMapcheck = System.currentTimeMillis();
        Iterator<Map.Entry<String, Double>> iterator = hashMap.entrySet().iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next().getValue());
        }
        System.out.println("##");
        System.out.println("hashMap遍历"+size+"条数据消耗时间为:"+(System.currentTimeMillis()-hashMapcheck));
        //清空hashMap,避免影响内存
        hashMap.clear();
        //清除hashmap需要时间,此过程会影响linkedhashmap的速度,确保hashmap清理完毕再执行
        Thread.sleep(3000);
        System.out.println("hashMap长度:"+hashMap.size());


        System.out.println("----开始测试linkHashMap---");
        LinkedHashMap<String, Double> linkedHashMap = new LinkedHashMap<>(size);
        long linkHashMapStart = System.currentTimeMillis();
        for(int i=0;i<size;i++){
            linkedHashMap.put(UUID.randomUUID().toString(),Math.random());
        }
        System.out.println("linkHashMap插入"+size+"条数据消耗时间为:"+(System.currentTimeMillis()-linkHashMapStart));
        long linkHashMapcheck = System.currentTimeMillis();
        Iterator<Map.Entry<String, Double>> iterator1 = linkedHashMap.entrySet().iterator();
        while (iterator1.hasNext()){
            System.out.print(iterator1.next().getValue());
        }
        System.out.println("##");
        System.out.println("linkHashMap遍历"+size+"条数据消耗时间为:"+(System.currentTimeMillis()-linkHashMapcheck));
    }
  • 写回答

5条回答 默认 最新

  • 好烦吃不胖 2021-04-01 15:08
    关注

    首先说一下你这个测试方法都是错的,什么叫查询?所谓的查询值得是从一堆数据中区检索到我想要的数据,而不是你所写的遍历,完全两码事。你查询总得有个被查的目标吧?遍历不等于查询!!!!!!!!

    给你的代码改了一下,我查询的目标是中间的那个,也就是索引为size/2的:

    public class QueryTest {
        public static void main(String[] args) {
            int size = 5000000;
            HashMap<String, Double> hashMap = new HashMap<>(size);
            System.out.println("----开始测试hashMap---");
            long hashMapStart = System.currentTimeMillis();
            for (int i = 0; i < size; i++) {
                hashMap.put(i + "", Math.random());
            }
            System.out.println("hashMap插入" + size + "条数据消耗时间为:" + (System.currentTimeMillis() - hashMapStart));
    
            long hashMapcheck = System.currentTimeMillis();
            for (int i = 0; i < size; i++) {
                hashMap.get(size/2 + "");
            }
            System.out.println("hashmap查询" + size + "次所耗时间" + (System.currentTimeMillis() - hashMapcheck));
    
            System.out.println("====================分割线==========================");
            System.out.println("----开始测试linkHashMap---");
            LinkedHashMap<String, Double> linkedHashMap = new LinkedHashMap<>(size);
            long linkHashMapStart = System.currentTimeMillis();
            for(int i=0;i<size;i++){
                linkedHashMap.put(i+"",Math.random());
            }
            System.out.println("linkHashMap插入"+size+"条数据消耗时间为:"+(System.currentTimeMillis()-linkHashMapStart));
            long linkHashMapcheck = System.currentTimeMillis();
            for (int i = 0; i < size; i++) {
                linkedHashMap.get(size/2 + "");
            }
            System.out.println("##");
            System.out.println("linkHashMap查询"+size+"次消耗时间为:"+(System.currentTimeMillis()-linkHashMapcheck));
        }
    
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月26日

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果