• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Java 中 Map 的5种遍历方式

java 来源:冬日温暖2015 1次浏览

同系列文章
java 中 List 的5种遍历方式
Java 中 Set 的4中遍历方式
Java 中 Map 的5种遍历方式

主测试方法

    @Test
    public void test1(){ 
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        int n = 1000_0000;
        for (int i = 1; i <= n; i++) { 
            map.put(i, i);
        }

        System.out.println("======== 1 entrySet 最常用 ==========");
        entrySetTest(map);
        System.out.println("======== 2 keySet Or Values 取keys或values 时,效率最高 ==========");
        keySetOrValuesTest(map);
        //System.out.println("======== keySetForValueTest ==========");
        keySetForValueTest(map);
        System.out.println("======== 3 iterator Type ==========");
        iteratorTypeTest(map);
        System.out.println("======== 4 forEach java 8 效率最低 ==========");
        forEachTest(map);
        System.out.println("======== 5 stream ForEach java 8 流式 ==========");
        streamForEachTest(map);

    }

1000万数据测试结果

======== 1 entrySet 最常用 ==========
map.entrySet() time=	65

======== 2 keySet Or Values 取keys或values 时,效率最高 ==========
map.keySet() time=	71
map.keySet().iterator() time=	63
map.values() time=	74
map.values().iterator() time=	64
map.keySet() -> map.get(key) time=	107

======== 3 iterator Type ==========
map.entrySet().iterator() time=	85

======== 4 forEach java 8 效率最低 ==========
map.forEach time=	103

======== 5 stream ForEach java 8 流式 ==========
map.entrySet().stream().forEach time=	62

1、最常用的方法,map.entrySet() 加增强for

    /** * 1 * entrySet 这是最常用的方式,在键值都需要的时候 * @param map */
    public void entrySetTest(Map<Integer, Integer> map){ 
        long before = System.currentTimeMillis();
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) { 
            entry.getKey();
            entry.getValue();
            // System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        long after = System.currentTimeMillis();
        System.out.println("map.entrySet() time=\t"  + (after - before));
    }

2、通过keySet() 或 values()获取

    /** * 2(1)keySet values * 如果只需要键或者值,这个比键值对的要快 * @param map */
    public void keySetOrValuesTest(Map<Integer, Integer> map){ 
        long before = System.currentTimeMillis();
        // 遍历map中的键
        for (Integer key : map.keySet()) { 
            //System.out.println("Key = " + key);
        }
        long after = System.currentTimeMillis();
        System.out.println("map.keySet() time=\t"  + (after - before));

        before = System.currentTimeMillis();
        // 遍历map中的键
        Iterator<Integer> iteratorKeySet = map.keySet().iterator();
        while (iteratorKeySet.hasNext()) { 
            iteratorKeySet.next();
            //System.out.println("key = " + iteratorKeySet.next());
        }

        after = System.currentTimeMillis();
        System.out.println("map.keySet().iterator() time=\t"  + (after - before));


        before = System.currentTimeMillis();
        // 遍历map中的值
        for (Integer value : map.values()) { 
            //System.out.println("Value = " + value);
        }
        after = System.currentTimeMillis();
        System.out.println("map.values() time=\t"  + (after - before));

        before = System.currentTimeMillis();
        // 遍历map中的值
        Iterator<Integer> iteratorValues = map.values().iterator();
        while (iteratorValues.hasNext()) { 
            iteratorValues.next();
            //System.out.println("key = " + iterator.next());
        }
        after = System.currentTimeMillis();
        System.out.println("map.values().iterator() time=\t"  + (after - before));
    }
    /** * 2(2)keySet get * 根据键找值,效率很低 * @param map */
    public void keySetForValueTest(Map<Integer, Integer> map){ 
        long before = System.currentTimeMillis();
        //遍历map中的键
        for (Integer key : map.keySet()) { 
            Integer value = map.get(key);
            //System.out.println("Key = " + key + ", Value = " + value);
        }
        long after = System.currentTimeMillis();
        System.out.println("map.keySet() -> map.get(key) time=\t"  + (after - before));
    }

3、通过迭代器 map.entrySet().iterator()

    /** * 3(1)iterator type * 迭代器 带泛型 * @param map */
    public void iteratorTypeTest(Map<Integer, Integer> map){ 
        long before = System.currentTimeMillis();
        Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
        while (entries.hasNext()) { 
            Map.Entry<Integer, Integer> entry = entries.next();
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        long after = System.currentTimeMillis();
        System.out.println("map.entrySet().iterator() time=\t"  + (after - before));
    }

4、map.forEach 注意:java8 才支持

    /** * 4 forEach 遍历的是键值,所以是两个参数 * forEach java8 lambda * @param map */
    public void forEachTest(Map<Integer, Integer> map){ 
        long before = System.currentTimeMillis();
        //map.forEach( (key,value) -> {System.out.println("Key = " + key+ ", Value = "+ value);} );
        map.forEach( (key,value) -> { } );
        long after = System.currentTimeMillis();
        System.out.println("map.forEach time=\t"  + (after - before));

        // 一条语句可以省略{}
        //map.forEach( (key,value) -> System.out.println("Key = " + key+ ", Value = "+ value) );
    }

5、map.entrySet().stream().forEach 注意:java8 才支持

    /** * 5 stream forEach 遍历的是entry 所以是一个参数 * stream forEach java8 lambda * @param map */
    public void streamForEachTest(Map<Integer, Integer> map){ 
        long before = System.currentTimeMillis();
        //map.entrySet().stream().forEach( (entry) -> {System.out.println("Key = " + entry.getKey()+ ", Value = "+ entry.getValue());} );
        //map.entrySet().stream().forEach( System.out::println);
        map.entrySet().stream().forEach( (entry) -> { } );
        long after = System.currentTimeMillis();
        System.out.println("map.entrySet().stream().forEach time=\t"  + (after - before));

        // 一个参数可以省略() 一条语句可以省略{}
        //map.entrySet().stream().forEach(entry -> System.out.println("Key = " + entry.getKey()+ ", Value = "+ entry.getValue()) );
    }

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)