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的,不用担心性能问题。

上一篇:题解 P5851 [USACO19DEC]Greedy Pie Eaters P


下一篇:38 时间和日期相关函数