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));
    }

執行結果:mybatis

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

讀取數據是有序,則以下:dom

    @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的,不用擔憂性能問題。spa

相關文章
相關標籤/搜索