首页 > 编程语言 >Java 中 Set 的4中遍历方式

Java 中 Set 的4中遍历方式

时间:2022-10-05 18:47:14浏览次数:81  
标签:遍历 Java iterator System set forEach println Set out

Set 和 List 遍历方式基本一致,Set 没有for的遍历方式


主测试方法

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

        System.out.println("======== 1 iterator ==========");
        iteratorTest(set);
        System.out.println("======== 2 增强for ==========");
        forBoostTest(set);
        System.out.println("======== 3 forEach 最耗时 java8 ==========");
        forEachTest(set);
        System.out.println("======== 4 stream forEach 比较耗时和增强for差不多 java8 ==========");
        streamForEachTest(set);
    }


测试结果
======== 1 iterator ==========
set.iterator() for time=	68
set.iterator() while time=	66

======== 2 增强for ==========
set.for Boost time=	75

======== 3 forEach 最耗时 java8 ==========
set.forEach time=	118

======== 4 stream forEach 比较耗时和增强for差不多 java8 ==========
set.stream().forEach() time=	71


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

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


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


3、set.forEach 注意:java8 才支持
    /**
     * 3 foreach
     * foreach 最慢不推荐 java8 lambda
     * @param set
     */
    public static void forEachTest(Set<Integer> set) {
        long before = System.currentTimeMillis();
        set.forEach(item -> {
            // System.out.println(item);
        });
        long after = System.currentTimeMillis();
        System.out.println("set.forEach time=\t"  + (after - before));
    }

4、set.stream().forEach 注意:java8 才支持
    /**
     * 4 stream foreach
     * stream foreach java8 特有
     * @param set
     */
    public static void streamForEachTest(Set<Integer> set) {
        long before = System.currentTimeMillis();
        set.stream().forEach(item -> {
            // System.out.println(item);
        });
        long after = System.currentTimeMillis();
        System.out.println("set.stream().forEach() time=\t"  + (after - before));
    }

标签:遍历,Java,iterator,System,set,forEach,println,Set,out
From: https://www.cnblogs.com/sxhxh/p/16756096.html

相关文章