Skip to main content
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

In detail, how does the 'for each' loop work in Java?

Consider:

List<String> someList = new ArrayList<>();
// add "monkey", "donkey", "skeleton key" to someList
for (String item : someList) {
    System.out.println(item);
}

What would the equivalent for loop look like without using the for each syntax?


People new to Java commonly encounter issues when trying to modify the original data using the new style foreach loop. Use Why doesn't assigning to the iteration variable in a foreach loop change the underlying data? to close duplicates about that common problem. Note that other languages with analogous constructs generally have the same issue; for example, see Why doesn't modifying the iteration variable affect subsequent iterations? for the same issue in Python.

Answer*

Cancel
15
  • 7
    This answer is now a blog post and was created from two related answers that I've written: here and here. It also includes a generically useful class for comparing the speed of two functions (at the bottom).
    aliteralmind
    –  aliteralmind
    2014-04-19 18:23:12 +00:00
    Commented Apr 19, 2014 at 18:23
  • 3
    Just one minor comment here, you shouldn't categorically state that for(:) syntax is always better for accessing collections; if you are using an array list, the for (:) loop will be about 2 x slower than using for (int i = 0, len = arrayList.size(); i < len; i++). I think you mentioned that in the [link]( stackoverflow.com/questions/2113216/…) link anyway, but is important to highlight that...
    Leo
    –  Leo
    2014-09-26 11:25:04 +00:00
    Commented Sep 26, 2014 at 11:25
  • 2
    It's important to compare apples-to-apples, here. The question was about for versus foreach. You are comparing indexed-access versus iterated access. It's important to point out that foreach is just syntactic sugar wrapping "iterator" traversal of the collection. There is no discernible difference between for and foreach when you are always using an iterator, so it's not fair to imply that foreach is better for performance. Technically speaking, it generates the same code as a regular for loop provided that you are still using an iterator in the for loop.
    Christopher Schultz
    –  Christopher Schultz
    2019-10-18 20:35:59 +00:00
    Commented Oct 18, 2019 at 20:35
  • 2
    To me saying that foreach is more performant when going through Collection is terribly misleading. If it is a linked list in a for loop using the get(i), you're not traversing once, you're traversing 1+2+..n times. Using iterable is to make sure to go through the Collection in the most optimal way without having to have to write extra code (ex : current = current.getChild()). Of course for ArrayList, you will be a bit faster using a for instead fo foreach because you don't need to construct the intermediary Iterator. A logical explanation is way better than benchmarks in this case for me
    Walfrat
    –  Walfrat
    2020-06-10 08:14:34 +00:00
    Commented Jun 10, 2020 at 8:14
  • 2
    Since a for-each loop over a primitive array is compiled to the same bytecode as an index based for loop over the same array, any test that measures a significant performance difference between these two source code forms is obviously broken.
    Holger
    –  Holger
    2024-10-02 10:53:55 +00:00
    Commented Oct 2, 2024 at 10:53

Morty Proxy This is a proxified and sanitized view of the page, visit original site.