1

I'm new in programming I've learned three ways of getting elements of a list and i know each of them is good in a specific situation but i want to know which one is better in overall ( performance )

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();

        for (int i = 0; i <= 15; i++) {
            arrayList.add(i + 666);
        }


        Iterator<Integer> iterator = arrayList.iterator();

        while (iterator.hasNext()) {
            System.out.println(iterator.next() + "Iterator");
        }

        System.out.println("==============");
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i) + " for ");
        }

        System.out.println("==============");
        for (int i : arrayList) {
            System.out.println(i + " for each");
        }
    }
}

thank you

Arty
  • 819
  • 3
  • 13
  • 24
Amirreza Yegane
  • 315
  • 1
  • 5
  • 14
  • 2
    like the answers say: Don't trouble yourself with performance issues. you rarely have to think about performance. here is a 4th example for Java8: `arrayList.forEach(System.out::println)` or `arrayList.forEach(item -> System.out.println(item + " Java 8"))` – Jack Flamp Mar 07 '18 at 12:40
  • 1
    Why don't you benchmark it for yourself? Fill the lists with 100'000 elements and measure times – André Schild Mar 07 '18 at 16:11

4 Answers4

2

Please don't compare them. You can't use all the styles all the time.

To start with, style 1 (while (...)) and style 3 (for-each) are same. Style 3 (for-each) uses style 1 (iterator and while) internally.

If you want to do operations on the list while iterating use the iterator style. It supports that.

You can't modify list in style 2 (simple for). If you just want to read the list and do not want to do anything apart from it go with style 2.

After all it's getting element from the underlying array using index. In traditional for loop, you are passing the index and incase of iterator, it keep on incrementing itself (index) internally.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

Don't think about performances. It is extremely on a mirco-management level that you would not even notice it. Besides that an Iterator is needed for iterating over a Collection. The enhanced for loop is a syntactical sugar, but it does the same as the Iterator. The only difference is that you explicitly need a Iterator to modify the Collection while looping over it. See How to avoid "ConcurrentModificationException" while removing elements from `ArrayList` while iterating it?

And lastly the range based loop is a way to limit the number of iteration while the other ones iterate over the whole Collection (except you add some conditions inside the loop block)

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
0

There is not any specified loop to access the elements of an array. under different conditions different loops are useful. mostly while loop is used to access elements of a list. but again it depends on the situation when to use which loop...and you already have a better knowledge of the difference of loops

0
for (int i: arrayList){
    System.out.println(i + " for each");
}

is the same as

Iterator<Integer> iterator = arrayList.iterator();

while (iterator.hasNext()){
    System.out.println(iterator.next() + "Iterator");
}

It is just syntactic sugar.

This

for (int i=0; i<arrayList.size(); i++){
    System.out.println(arrayList.get(i) + " for ");
}

is probably a little better, but only for ArrayList, but should not actually make measurable difference, as the Iterator doesn't do anything different than this. For other List implementations however, which don't have O(1) get(int index), this is much worse (LinkedList).

kutschkem
  • 7,826
  • 3
  • 21
  • 56