Which one is the best way to repeat the elements of a particular collection?

I always use for each cycle when viewing the elements of a particular collection .

To check how much time each process of the loop takes, I encoded this way

public class LoopingDemo {
    static ArrayList<String> arrayList = new ArrayList<String>();
    static double start;
    static double end;

    public static void iterator() {
        simpleForLoop();
        System.out.println();
        forEachLoop();
        System.out.println();
        useWhileLoop(arrayList);
        System.out.println();
        useForLoop(arrayList);
        System.out.println();
        enumerator();
    }
    public static void simpleForLoop(){
        start = System.nanoTime();
        for(int i=0;i<arrayList.size();i++){
            String str = arrayList.get(i);
            System.out.println(": "+str);
        }
        end = System.nanoTime();
        System.out.println("Taken taken in simpleForLoop process: "
            + (end - start));
    }

    public static void forEachLoop() {
        start = System.nanoTime();
        for (String str : arrayList) {
        System.out.println(str);
    }
        end = System.nanoTime();
        System.out.println("Taken taken in forEachLoop process: "
            + (end - start));
    }

    public static void enumerator() {
        start = System.nanoTime();

        // get the Enumeration object
        Enumeration<String> en = Collections.enumeration(arrayList);

        // enumerate through the ArrayList elements
        System.out.println("Enumerating through Java ArrayList");

        while (en.hasMoreElements()) {
            System.out.println(en.nextElement());
            /*
             * String name = (String) en.nextElement();
             * System.out.println(name);
             */
        } 
        end = System.nanoTime();
        System.out.println("Taken taken in enumeration process: "
            + (end - start));
    }

    private static void useWhileLoop(Collection<String> myList) {
        start = System.nanoTime();
        Iterator<String> itr = myList.iterator();
        while (itr.hasNext()) {
            String str = itr.next(); // Returns the next element in the
                                    // iteration.
            System.out.println(str);
            // System.out.println(itr.next()); // in one line
        }
        end = System.nanoTime();
        System.out.println("Taken taken in useWhileLoop process: "
            + (end - start));
    }

    /**
     * Note that this for-loop does not use an integer index.
     */
    private static void useForLoop(Collection<String> myList) {
        start = System.nanoTime();
        for (Iterator<String> itr = myList.iterator(); itr.hasNext();) {
            System.out.println(itr.next());
        }
        end = System.nanoTime();
        System.out.println("Taken taken in useForLoopWithIterator process: "
            + (end - start));
    }

    public static void addElements() {

        // Add elements to the array list.
        arrayList.add("C");
        arrayList.add("A");
        arrayList.add("E");
        arrayList.add("B");
        arrayList.add("D");
        arrayList.add("F");
        arrayList.add(1, "A2");

    }

    public static void main(String[] args) {
        addElements();
        iterator();

    }

}

Surprisingly, the cycle executed by using for each cycle lags behind the simple cycle for the cycle . (Results may vary for different machines with different configurations.)

Console exit:

Taken taken in simpleForLoop process:              853200.0
Taken taken in forEachLoop process:                788993.0
Taken taken in useWhileLoop process:               452014.0
Taken taken in useForLoopWithIterator process:     299775.0
Taken taken in enumeration process:                766756.0

So why do people prefer to do this through a loop for everyone ? Is there a performance based reason?

+4
4

IIRC, - , . .

, , . "for loop with iterator" , .

, , , , ( 5 6- ), :

Taken taken in useForLoopWithIterator process: 105110.0
Taken taken in simpleForLoop process: 122181.0
Taken taken in useWhileLoop process: 104774.0
Taken taken in enumeration process: 123520.0
Taken taken in forEachLoop process: 106782.0

forEachloop, useForLoopWithIterator useWhileLoop ( ). , . , , , . LinkedList.

+1

. "for ( e: set).

0

, /. , . , .

0

This book discusses efficient Java. Basically, foreach is concise and elegant for those who read and understand the code. He always said that it’s premature not to optimize. Just look at the optimization when you are absolutely sure that it is necessary. Therefore, if you find that you can often find foreach, because it was because at that moment it was not easy to understand, maintain and optimize.

0
source

All Articles