编程语言
首页 > 编程语言> > java性能优化之HashMap,LinkedHashMap,TreeMap读取大量数据效率的比较

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

标签:count,time1,java,HashMap,int,System,time2,TreeMap,++
来源: https://www.cnblogs.com/zhjh256/p/11626524.html