Java 中 List 的5种遍历方式

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

本测试没有具体业务,单纯遍历,也许会有误差。

主单元测试方法

    @Test
    public void test(){
        List<Integer> list = new ArrayList<>();
        int n = 1000_0000;
        for (int i = 0; i < n; i++) {
            list.add(i);
        }

        System.out.println("======== 1 fori 推荐 ==========");
        foriTest(list);
        System.out.println("======== 2 iterator ==========");
        iteratorTest(list);
        System.out.println("======== 3 增强for ==========");
        forBoostTest(list);
        System.out.println("======== 4 forEach 最耗时 java8 ==========");
        forEachTest(list);
        System.out.println("======== 5 stream forEach 比较耗时和增强for差不多 java8 ==========");
        streamForEachTest(list);
        System.out.println("======== 6 parallelStream  java8 ==========");
        parallelStreamForEachTest(list);
    }

测试1000万数据测试结果

======== 1 fori 推荐 ==========
for i time=	7
======== 2 iterator ==========
iterator for time=	8
iterator while time=	8
======== 3 增强for ==========
for Boost time=	22
======== 4 forEach 最耗时 java8 ==========
list.forEach time=	69
======== 5 stream forEach 比较耗时和增强for差不多 java8 ==========
list.stream().forEach time=	23
======== 6 parallelStream forEach 并行 java8 ==========
list.parallelStream().forEach time=	28

1、普通fori 方式遍历

    /**
     * 1
     * 普通fori
     * 效率最高,也是java官方推荐的用法
     *
     * @param list
     */
    public static void foriTest(List<Integer> list) {
        long before = System.currentTimeMillis();
        int j = list.size();
        for (int i = 0; i < j; i++) {
            list.get(i);
        }
        long after = System.currentTimeMillis();
        System.out.println("for i time=\t"  + (after - before));
    }

2、iterate()迭代器方式(collection 集合共有方法)

    /**
     * 2 iterator
     * 迭代器
     * @param list
     */
    public static void iteratorTest(List<Integer> list) {
        long before = System.currentTimeMillis();
        for (Iterator<Integer> iterator = list.iterator(); iterator.hasNext(); ) {
            iterator.next();
        }
        long after = System.currentTimeMillis();
        System.out.println("iterator for time=\t"  + (after - before));

        before = System.currentTimeMillis();
        /**
         * while 循环写法
         */
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            iterator.next();
        }
        after = System.currentTimeMillis();
        System.out.println("iterator while time=\t"  + (after - before));
    }

3、增强for

    /**
     * 3
     * 增强for
     * @param list
     */
    public static void forBoostTest(List<Integer> list) {
        long before = System.currentTimeMillis();
        for (int item : list) {
            // System.out.println(item);
        }
        long after = System.currentTimeMillis();
        System.out.println("for Boost time=\t"  + (after - before));
    }

4、forEach() 注意java8 才支持

    /**
     * 4 foreach
     * foreach 最慢不推荐 java8 lambda
     * @param list
     */
    public static void forEachTest(List<Integer> list) {
        long before = System.currentTimeMillis();
        list.forEach(item -> {
            // System.out.println(item);
        });
        long after = System.currentTimeMillis();
        System.out.println("list.forEach time=\t"  + (after - before));
    }

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

    /**
     * 5 stream foreach
     * stream foreach java8 特有
     * @param list
     */
    public static void streamForEachTest(List<Integer> list) {
        long before = System.currentTimeMillis();
        list.stream().forEach(item -> {
            // System.out.println(item);
        });
        long after = System.currentTimeMillis();
        System.out.println("list.stream().forEach time=\t"  + (after - before));
    }

6、parallelStream().forEach() 注意java8 才支持

    /**
     * 5 stream foreach
     * stream foreach java8 特有
     * @param list
     */
    public static void parallelStreamForEachTest(List<Integer> list) {
        long before = System.currentTimeMillis();
        list.parallelStream().forEach(item -> {
            // System.out.println(item);
        });
        long after = System.currentTimeMillis();
        System.out.println("list.parallelStream().forEach time=\t"  + (after - before));
    }

你可能感兴趣的:(Java)