GeekyDaddy
GeekyDaddy

Reputation: 384

Looping through lists, better method

As I’m coding and discovering new ways of doing things in Java, I’m always somewhat perplexed in the better method of looping through lists to output data.

In the following example, I’m looping through lists and using a counter, so many times I’ve had to include an index counter in the output.

I'm partial to Method 1 but I found any of these method a bit dated. I’ve seen many examples of looping through lists and Method 2 is mostly used.

So my question is what is the better method, if all of these methods are just as equal, then what is the most standard?

private ArrayList<String> list = new ArrayList<String>();

public Test() {
    list.add("One");        
    list.add("Two");        
    list.add("Three");      
    list.add("Four");       
    list.add("Five");

    method1();
    method2();
    method3();
}

public void method1() {
    System.out.println("Method 1");
    int i = 1;
    for (String value:list) {
        System.out.println((i++) + " = " + value);
    }
}

public void method2() {
    System.out.println("Method 2");
    for (int i = 0; i < list.size(); i++) {
        System.out.println((i+1) + " = " + list.get(i));
    }
}

public void method3() {
    System.out.println("Method 3");
    Iterator<String> it = list.iterator();
    int i = 1;
    while (it.hasNext()) {
        System.out.println((i++) + " = " + it.next());
    }
}

Upvotes: 9

Views: 1051

Answers (4)

Ganesh chaitanya
Ganesh chaitanya

Reputation: 658

method3() where Iterator is used is the best way to iterate through a list. Even though method1() uses Iterator behind the scenes, if you want to modify the list inside the loop, like update or delete, it might lead to ConcurrentModificationException.

Upvotes: -1

suranjan
suranjan

Reputation: 447

The first is better and least error prone.

public void method1() {
    System.out.println("Method 1");
    int i = 1;
    for (String value:list) {
        System.out.println((i++) + " = " + value);
    }
}

The second options is tightly coupled with the Collection you are using. It means, if someone changes the data structures then he/she has to change the code for the for loop as well.

The third option, Iterators can get ugly when you have to use nested loops and have to deal with multiple iterators.

Have a look at following link to see the error prone usage of iterators. https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html

Upvotes: 2

Alexis C.
Alexis C.

Reputation: 93892

method1() is similar to method3() as the for-each loop uses the List's iterator behind the scenes. The difference with method3() is that you actually have access to this iterator, so you can call remove on it if you wish to remove elements from the list.

method2()on the other hand can lead to "bad" performances depending on the underlying implementation. If your list is a LinkedList, get has O(n) complexity time so the for-loop will have O(n^2) complexity. With the iterator, you'll always get the next element in constant time.

I would personally use 1, it's also less code to write and this is one of the main benefit of the for-each loop if your intent is to perform a read-only operation on your data structure.

If you are using Java 8 and you don't need to print the index, you could also do:

list.forEach(System.out::println);

Upvotes: 18

NimChimpsky
NimChimpsky

Reputation: 47300

None of them. Use lambda expressions, available in java 8.

Or Guava functions if using Java < 8.

Upvotes: -3

Related Questions