java性能优化之HashMap,LinkedHashMap,TreeMap读取大量数据效率的比较

Posted 太白的技术博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java性能优化之HashMap,LinkedHashMap,TreeMap读取大量数据效率的比较相关的知识,希望对你有一定的参考价值。

很多时候,我们用jdbctemplate或mybatis的时候,为了查询通用,会选择使用map数据结构,因为hashmap本身无序,所以为了保证key的有序性,会采用linkedhashmap。所以我们要看一下Linkedhashmap的性能影响多大。如下:

    @Test
    public void test() {
        Integer count =1000000;
        Random random =new Random();
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < count; i++) {
            map.put(i+"", i+"");
        }
        long time1 = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            map.get((random.nextInt(count)+1)+"");
        }
        long time2 = System.currentTimeMillis();
        System.out.println("HashMap time:" + (time2 - time1));
        ////////////////////////////////////////////////////////////////////////
        Map<String, String> linkedMap = new LinkedHashMap<String, String>();
        for (int i = 0; i < count; i++) {
            linkedMap.put(i+"", i+"");
        }
 
        time1 = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
             linkedMap.get((random.nextInt(count)+1)+"");
        }
        time2 = System.currentTimeMillis();
        System.out.println("LinkedHashMap time:" + (time2 - time1));
                ////////////////////////////////////////////////////////////////////////
        Map<String, String> treeMap = new TreeMap<String, String>();
        for (int i = 0; i < count; i++) {
            treeMap.put(i+"", i+"");
        }
 
        time1 = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            treeMap.get((random.nextInt(count)+1)+"");
        }
        time2 = System.currentTimeMillis();
        System.out.println("TreeMap time:" +  (time2 - time1));
    }

执行结果:

HashMap time:641
LinkedHashMap time:703
TreeMap time:4040

读取数据是有序,则如下:

    @Test
    public void test2() {
        Integer count =1000000;
        Random random =new Random();
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < count; i++) {
            map.put(i+"", i+"");
        }
        long time1 = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            map.get(i+"");
        }
        long time2 = System.currentTimeMillis();
        System.out.println("HashMap time:" + (time2 - time1));
        ////////////////////////////////////////////////////////////////////////
        Map<String, String> linkedMap = new LinkedHashMap<String, String>();
        for (int i = 0; i < count; i++) {
            linkedMap.put(i+"", i+"");
        }
 
        time1 = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
             linkedMap.get(i+"");
        }
        time2 = System.currentTimeMillis();
        System.out.println("LinkedHashMap time:" + (time2 - time1));
        ////////////////////////////////////////////////////////////////////////
        Map<String, String> treeMap = new TreeMap<String, String>();
        for (int i = 0; i < count; i++) {
            treeMap.put(i+"", i+"");
        }
 
        time1 = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            treeMap.get(i+"");
        }
        time2 = System.currentTimeMillis();
        System.out.println("TreeMap time:" +  (time2 - time1));
    }

HashMap time:297
LinkedHashMap time:203
TreeMap time:438

从上可知,LinkedHashMap是可以完全代替HashMap的,不用担心性能问题。

以上是关于java性能优化之HashMap,LinkedHashMap,TreeMap读取大量数据效率的比较的主要内容,如果未能解决你的问题,请参考以下文章

Java8之HashMap源码分析

Android应用性能优化之使用SparseArray替代HashMap

Java HashMap 性能优化/替代

HashMap Performance Improvements in Java 8

JDK1.8 源码分析之HashMap

Java集合详解9:2021年的hashmap面试题怎么考?涵盖性能优化扩容并发问题等核心考点