码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-10-06 11:06:34      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:性能优化   out   temp   比较   public   edm   ++   oid   count   

很多时候,我们用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读取大量数据效率的比较

标签:性能优化   out   temp   比较   public   edm   ++   oid   count   

原文地址:https://www.cnblogs.com/zhjh256/p/11626524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!