ChatGPT解决这个技术问题 Extra ChatGPT

How does the Java 'for each' loop work?

Consider:

List<String> someList = new ArrayList<String>();
// 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?

As per JLS it has two forms: stackoverflow.com/a/33232565/1216775
There are different types of Java for loop as mentioned below. The working of for loop is the same for all the types and varies only in the syntax. * Simple for loop (tutorialcup.com/java/…) * Enhanced for loop – for each loop (tutorialcup.com/java/java-for-loop.htm#Enhanced_Java_For_loop) * Nested for loop (tutorialcup.com/java/java-for-loop.htm#Nested_For_Loop) * Labeled for loop (tutorialcup.com/java/java-for-loop.htm#Labeled_For_loop) tutorialcup.com/java/java-for-loop.htm
We can use enhanced for loop to iterate elements for the below collection: Array ArrayList Map Set LinkedList and so on. tutorialcup.com/java/java-for-each-loop.htm

S
Sotirios Delimanolis
for (Iterator<String> i = someIterable.iterator(); i.hasNext();) {
    String item = i.next();
    System.out.println(item);
}

Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for ( : ) idiom, since the actual iterator is merely inferred.

As was noted by Denis Bueno, this code works for any object that implements the Iterable interface.

Also, if the right-hand side of the for (:) idiom is an array rather than an Iterable object, the internal code uses an int index counter and checks against array.length instead. See the Java Language Specification.


I've found just calling a while-loop like while (someList.hasMoreElements()) { //do something }} - gets me close to the coding grace I'd hoped to find when I searched for this question.
P
Peter Mortensen

The construct for each is also valid for arrays. e.g.

String[] fruits = new String[] { "Orange", "Apple", "Pear", "Strawberry" };

for (String fruit : fruits) {
    // fruit is an element of the `fruits` array.
}

which is essentially equivalent of

for (int i = 0; i < fruits.length; i++) {
    String fruit = fruits[i];
    // fruit is an element of the `fruits` array.
}

So, overall summary:
[nsayer] The following is the longer form of what is happening:

for(Iterator i = someList.iterator(); i.hasNext(); ) { String item = i.next(); System.out.println(item); } Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for( : ) idiom, since the actual Iterator is merely inferred.

[Denis Bueno]

It's implied by nsayer's answer, but it's worth noting that the OP's for(..) syntax will work when "someList" is anything that implements java.lang.Iterable -- it doesn't have to be a list, or some collection from java.util. Even your own types, therefore, can be used with this syntax.


I wouldn't say the 2nd example is "essentially equivalent to the first", since if its a list of primitive values, any modification you make to the values will not modify the original list in example 1, but will modify the original list in example 2.
P
Peter Mortensen

The for-each loop, added in Java 5 (also called the "enhanced for loop"), is equivalent to using a java.util.Iterator--it's syntactic sugar for the same thing. Therefore, when reading each element, one by one and in order, a for-each should always be chosen over an iterator, as it is more convenient and concise.

For-each

for (int i : intList) {
   System.out.println("An element in the list: " + i);
}

Iterator

Iterator<Integer> intItr = intList.iterator();
while (intItr.hasNext()) {
   System.out.println("An element in the list: " + intItr.next());
}

There are situations where you must use an Iterator directly. For example, attempting to delete an element while using a for-each can (will?) result in a ConcurrentModificationException.

For-each vs. for-loop: Basic differences

The only practical difference between for-loop and for-each is that, in the case of indexable objects, you do not have access to the index. An example when the basic for-loop is required:

for (int i = 0; i < array.length; i++) {
   if(i < 5) {
      // Do something special
   }  else {
      // Do other stuff
   }
}

Although you could manually create a separate index int-variable with for-each,

int idx = -1;
for (int i : intArray) {
   idx++;
   ...
}

...it is not recommended, since variable-scope is not ideal, and the basic for loop is simply the standard and expected format for this use case.

For-each vs. for-loop: Performance

When accessing collections, a for-each is significantly faster than the basic for loop's array access. When accessing arrays, however--at least with primitive and wrapper-arrays--access via indexes is dramatically faster.

Timing the difference between iterator and index access for primitive int-arrays

Indexes are 23-40 percent faster than iterators when accessing int or Integer arrays. Here is the output from the testing class at the bottom of this post, which sums the numbers in a 100-element primitive-int array (A is iterator, B is index):

[C:\java_code\]java TimeIteratorVsIndexIntArray 1000000
Test A: 358,597,622 nanoseconds
Test B: 269,167,681 nanoseconds
B faster by 89,429,941 nanoseconds (24.438799231635727% faster)

[C:\java_code\]java TimeIteratorVsIndexIntArray 1000000
Test A: 377,461,823 nanoseconds
Test B: 278,694,271 nanoseconds
B faster by 98,767,552 nanoseconds (25.666236154695838% faster)

[C:\java_code\]java TimeIteratorVsIndexIntArray 1000000
Test A: 288,953,495 nanoseconds
Test B: 207,050,523 nanoseconds
B faster by 81,902,972 nanoseconds (27.844689860906513% faster)

[C:\java_code\]java TimeIteratorVsIndexIntArray 1000000
Test A: 375,373,765 nanoseconds
Test B: 283,813,875 nanoseconds
B faster by 91,559,890 nanoseconds (23.891659337194227% faster)

[C:\java_code\]java TimeIteratorVsIndexIntArray 1000000
Test A: 375,790,818 nanoseconds
Test B: 220,770,915 nanoseconds
B faster by 155,019,903 nanoseconds (40.75164734599769% faster)

[C:\java_code\]java TimeIteratorVsIndexIntArray 1000000
Test A: 326,373,762 nanoseconds
Test B: 202,555,566 nanoseconds
B faster by 123,818,196 nanoseconds (37.437545972215744% faster)

I also ran this for an Integer array, and indexes are still the clear winner, but only between 18 and 25 percent faster.

For collections, iterators are faster than indexes

For a List of Integers, however, iterators are the clear winner. Just change the int-array in the test-class to:

List<Integer> intList = Arrays.asList(new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100});

And make the necessary changes to the test-function (int[] to List<Integer>, length to size(), etc.):

[C:\java_code\]java TimeIteratorVsIndexIntegerList 1000000
Test A: 3,429,929,976 nanoseconds
Test B: 5,262,782,488 nanoseconds
A faster by 1,832,852,512 nanoseconds (34.326681820485675% faster)

[C:\java_code\]java TimeIteratorVsIndexIntegerList 1000000
Test A: 2,907,391,427 nanoseconds
Test B: 3,957,718,459 nanoseconds
A faster by 1,050,327,032 nanoseconds (26.038700083921256% faster)

[C:\java_code\]java TimeIteratorVsIndexIntegerList 1000000
Test A: 2,566,004,688 nanoseconds
Test B: 4,221,746,521 nanoseconds
A faster by 1,655,741,833 nanoseconds (38.71935684115413% faster)

[C:\java_code\]java TimeIteratorVsIndexIntegerList 1000000
Test A: 2,770,945,276 nanoseconds
Test B: 3,829,077,158 nanoseconds
A faster by 1,058,131,882 nanoseconds (27.134122749113843% faster)

[C:\java_code\]java TimeIteratorVsIndexIntegerList 1000000
Test A: 3,467,474,055 nanoseconds
Test B: 5,183,149,104 nanoseconds
A faster by 1,715,675,049 nanoseconds (32.60101667104192% faster)

[C:\java_code\]java TimeIteratorVsIndexIntList 1000000
Test A: 3,439,983,933 nanoseconds
Test B: 3,509,530,312 nanoseconds
A faster by 69,546,379 nanoseconds (1.4816434912159906% faster)

[C:\java_code\]java TimeIteratorVsIndexIntList 1000000
Test A: 3,451,101,466 nanoseconds
Test B: 5,057,979,210 nanoseconds
A faster by 1,606,877,744 nanoseconds (31.269164666060377% faster)

In one test they're almost equivalent, but with collections, iterator wins.

*This post is based on two answers I wrote on Stack Overflow:

Uses and syntax for for-each loop in Java

Should I use an Iterator or a forloop to iterate?

Some more information: Which is more efficient, a for-each loop, or an iterator?

The full testing class

I created this compare-the-time-it-takes-to-do-any-two-things class after reading this question on Stack Overflow:

import  java.text.NumberFormat;
import  java.util.Locale;

/**
   &lt;P&gt;{@code java TimeIteratorVsIndexIntArray 1000000}&lt;/P&gt;

   @see  &lt;CODE&gt;&lt;A HREF=&quot;https://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java&quot;&gt;https://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java&lt;/A&gt;&lt;/CODE&gt;
 **/
public class TimeIteratorVsIndexIntArray {

    public static final NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);

    public static final void main(String[] tryCount_inParamIdx0) {
        int testCount;

        // Get try-count from a command-line parameter
        try {
           testCount = Integer.parseInt(tryCount_inParamIdx0[0]);
        }
        catch(ArrayIndexOutOfBoundsException | NumberFormatException x) {
           throw  new IllegalArgumentException("Missing or invalid command line parameter: The number of testCount for each test. " + x);
        }

        //Test proper...START
        int[] intArray = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100};

        long lStart = System.nanoTime();
        for(int i = 0; i < testCount; i++) {
           testIterator(intArray);
        }

        long lADuration = outputGetNanoDuration("A", lStart);

        lStart = System.nanoTime();
        for(int i = 0; i < testCount; i++) {
           testFor(intArray);
        }

        long lBDuration = outputGetNanoDuration("B", lStart);

        outputGetABTestNanoDifference(lADuration, lBDuration, "A", "B");
    }

    private static final void testIterator(int[] int_array) {
       int total = 0;
       for(int i = 0; i < int_array.length; i++) {
          total += int_array[i];
       }
    }

    private static final void testFor(int[] int_array) {
       int total = 0;
       for(int i : int_array) {
          total += i;
       }
    }
    //Test proper...END

    //Timer testing utilities...START
    public static final long outputGetNanoDuration(String s_testName, long l_nanoStart) {
        long lDuration = System.nanoTime() - l_nanoStart;
        System.out.println("Test " + s_testName + ": " + nf.format(lDuration) + " nanoseconds");
        return  lDuration;
    }

    public static final long outputGetABTestNanoDifference(long l_aDuration, long l_bDuration, String s_aTestName, String s_bTestName) {
        long lDiff = -1;
        double dPct = -1.0;
        String sFaster = null;
        if(l_aDuration > l_bDuration) {
            lDiff = l_aDuration - l_bDuration;
            dPct = 100.00 - (l_bDuration * 100.0 / l_aDuration + 0.5);
            sFaster = "B";
        }
        else {
            lDiff = l_bDuration - l_aDuration;
            dPct = 100.00 - (l_aDuration * 100.0 / l_bDuration + 0.5);
            sFaster = "A";
        }
        System.out.println(sFaster + " faster by " + nf.format(lDiff) + " nanoseconds (" + dPct + "% faster)");
        return  lDiff;
   }

   //Timer testing utilities...END

}

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).
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...
I imagine the for(int value : int_array) {/* loop content */} is slowest in your test because it is syntactically equivalent to for(int i = 0; i < int_array.length; i++) {int value = int_array[i]; /* loop content */}, which isn't what your test compares.
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.
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
P
Peter Mortensen

Here is an answer which does not assume knowledge of Java Iterators. It is less precise, but it is useful for education.

While programming we often write code that looks like the following:

char[] grades = ....
for(int i = 0; i < grades.length; i++) {   // for i goes from 0 to grades.length
    System.out.print(grades[i]);           // Print grades[i]
}

The foreach syntax allows this common pattern to be written in a more natural and less syntactically noisy way.

for(char grade : grades) {   // foreach grade in grades
    System.out.print(grade); // print that grade
}

Additionally this syntax is valid for objects such as Lists or Sets which do not support array indexing, but which do implement the Java Iterable interface.


P
Peter Mortensen

The for-each loop in Java uses the underlying iterator mechanism. So it's identical to the following:

Iterator<String> iterator = someList.iterator();

while (iterator.hasNext()) {
  String item = iterator.next();
  System.out.println(item);
}

This is what people actually used before for-each, not the C-style for statement.
P
Peter Mortensen

As defined in JLS, a for-each loop can have two forms:

If the type of expression is a subtype of Iterable then translation is as: List someList = new ArrayList(); someList.add("Apple"); someList.add("Ball"); for (String item : someList) { System.out.println(item); } // Is translated to: for(Iterator stringIterator = someList.iterator(); stringIterator.hasNext(); ) { String item = stringIterator.next(); System.out.println(item); } If the expression necessarily has an array type T[] then: String[] someArray = new String[2]; someArray[0] = "Apple"; someArray[1] = "Ball"; for(String item2 : someArray) { System.out.println(item2); } // Is translated to: for (int i = 0; i < someArray.length; i++) { String item2 = someArray[i]; System.out.println(item2); }

Java 8 has introduced streams which perform generally better with a decent size dataset. We can use them as:

someList.stream().forEach(System.out::println);
Arrays.stream(someArray).forEach(System.out::println);

The most relevant and accurate answer. Enchansed for has two translations indeed.
I entirely disagree with: Java 8 has introduced streams which perform generally better., streams are only really feasible if you work with a big enough data set. Also .stream().forEach() can be replaced with .forEach()
I'm not so thinking about .forEach(), because the run times of forEeach more than for loop, so that's forEarch don't best solutions
P
Peter Mortensen

It's implied by nsayer's answer, but it's worth noting that the OP's for(..) syntax will work when "someList" is anything that implements java.lang.Iterable -- it doesn't have to be a list, or some collection from java.util. Even your own types, therefore, can be used with this syntax.


fd is right - the internal code when the right-hand side of the for(:) idiom uses an int and array.length instead of fetching an Iterator. forums.sun.com/thread.jspa?messageID=2743233
P
Peter Mortensen

A foreach loop syntax is:

for (type obj:array) {...}

Example:

String[] s = {"Java", "Coffe", "Is", "Cool"};
for (String str:s /*s is the array*/) {
    System.out.println(str);
}

Output:

Java
Coffe
Is
Cool

WARNING: You can access array elements with the foreach loop, but you can NOT initialize them. Use the original for loop for that.

WARNING: You must match the type of the array with the other object.

for (double b:s) // Invalid-double is not String

If you want to edit elements, use the original for loop like this:

for (int i = 0; i < s.length-1 /*-1 because of the 0 index */; i++) {
    if (i==1) //1 because once again I say the 0 index
        s[i]="2 is cool";
    else
        s[i] = "hello";
}

Now if we dump s to the console, we get:

hello
2 is cool
hello
hello

R
Ryan Delucchi

The Java "for-each" loop construct will allow iteration over two types of objects:

T[] (arrays of any type)

java.lang.Iterable

The Iterable<T> interface has only one method: Iterator<T> iterator(). This works on objects of type Collection<T> because the Collection<T> interface extends Iterable<T>.


J
Jrovalle

In Java 8 features you can use this:

List<String> messages = Arrays.asList("First", "Second", "Third");

void forTest(){
    messages.forEach(System.out::println);
}

Output

First
Second
Third

this random information doesn't even remotely answer the question
P
Peter Mortensen

The concept of a foreach loop as mentioned in Wikipedia is highlighted below:

Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read.

So the concept of a foreach loop describes that the loop does not use any explicit counter which means that there is no need of using indexes to traverse in the list thus it saves user from off-by-one error. To describe the general concept of this off-by-one error, let us take an example of a loop to traverse in a list using indexes.

// In this loop it is assumed that the list starts with index 0
for(int i=0; i<list.length; i++){

}

But suppose if the list starts with index 1 then this loop is going to throw an exception as it will found no element at index 0 and this error is called an off-by-one error. So to avoid this off-by-one error the concept of a foreach loop is used. There may be other advantages too, but this is what I think is the main concept and advantage of using a foreach loop.


How does this answer the question? "What would the equivalent for loop look like without using the for each syntax?"
v
vivekkurien

In Java 8, they introduced forEach. Using it List, Maps can be looped.

Loop a List using for each

List<String> someList = new ArrayList<String>();
someList.add("A");
someList.add("B");
someList.add("C");

someList.forEach(listItem -> System.out.println(listItem))

or

someList.forEach(listItem-> {
     System.out.println(listItem); 
});

Loop a Map using for each

Map<String, String> mapList = new HashMap<>();
    mapList.put("Key1", "Value1");
    mapList.put("Key2", "Value2");
    mapList.put("Key3", "Value3");

mapList.forEach((key,value)->System.out.println("Key: " + key + " Value : " + value));

or

mapList.forEach((key,value)->{
    System.out.println("Key : " + key + " Value : " + value);
});

H
Hank

Here's an equivalent expression.

for(Iterator<String> sit = someList.iterator(); sit.hasNext(); ) {
    System.out.println(sit.next());
}

P
Peter Mortensen

Using older Java versions, including Java 7, you can use a foreach loop as follows.

List<String> items = new ArrayList<>();
items.add("A");
items.add("B");
items.add("C");
items.add("D");
items.add("E");

for(String item : items) {
    System.out.println(item);
}

The following is the very latest way of using a for each loop in Java 8 (loop a List with forEach + lambda expression or method reference).

Lambda

// Output: A,B,C,D,E
items.forEach(item->System.out.println(item));

Method reference

// Output: A,B,C,D,E
items.forEach(System.out::println);

For more information, refer to "Java 8 forEach examples".


R
Roger Lindsjö
for (Iterator<String> itr = someList.iterator(); itr.hasNext(); ) {
   String item = itr.next();
   System.out.println(item);
}

b
billjamesdev

Also note that using the "foreach" method in the original question does have some limitations, such as not being able to remove items from the list during the iteration.

The new for-loop is easier to read and removes the need for a separate iterator, but is only really usable in read-only iteration passes.


In those cases, the use of removeIf might be the right tool
P
Peter Mortensen

An alternative to forEach in order to avoid your "for each":

List<String> someList = new ArrayList<String>();

Variant 1 (plain):

someList.stream().forEach(listItem -> {
    System.out.println(listItem);
});

Variant 2 (parallel execution (faster)):

someList.parallelStream().forEach(listItem -> {
    System.out.println(listItem);
});

Should have mention that it's added in Java 1.8
We cant be 100% sure that the same thread is used. I like to use the for( form if I like to make sure the same thread is used. I like to use the stream-form if I like to allow multithreaded execution.
P
Peter Mortensen

It adds beauty to your code by removing all the basic looping clutter. It gives a clean look to your code, justified below.

Normal for loop:

void cancelAll(Collection<TimerTask> list) {
    for (Iterator<TimerTask> i = list.iterator(); i.hasNext();)
         i.next().cancel();
}

Using for-each:

void cancelAll(Collection<TimerTask> list) {
    for (TimerTask t : list)
        t.cancel();
}

for-each is a construct over a collection that implements Iterator. Remember that, your collection should implement Iterator; otherwise you can't use it with for-each.

The following line is read as "for each TimerTask t in list."

for (TimerTask t : list)

There is less chance for errors in case of for-each. You don't have to worry about initializing the iterator or initializing the loop counter and terminating it (where there is scope for errors).


P
Peter Mortensen

Prior to Java 8, you need to use the following:

Iterator<String> iterator = someList.iterator();

while (iterator.hasNext()) {
    String item = iterator.next();
    System.out.println(item);
}

However, with the introduction of Streams in Java 8 you can do same thing in much less syntax. For example, for your someList you can do:

someList.stream().forEach(System.out::println);

You can find more about streams here.


The foreach loop, added in Java 5 (also called the "enhanced for loop"), is equivalent to using a java.util.Iterator
P
Peter Mortensen

It would look something like this. Very crufty.

for (Iterator<String> i = someList.iterator(); i.hasNext(); )
        System.out.println(i.next());

There is a good writeup on for each in the Sun documentation.


g
gomisha

The Java for each loop (aka enhanced for loop) is a simplified version of a for loop. The advantage is that there is less code to write and less variables to manage. The downside is that you have no control over the step value and no access to the loop index inside the loop body.

They are best used when the step value is a simple increment of 1 and when you only need access to the current loop element. For example, if you need to loop over every element in an array or Collection without peeking ahead or behind the current element.

There is no loop initialization, no boolean condition and the step value is implicit and is a simple increment. This is why they are considered so much simpler than regular for loops.

Enhanced for loops follow this order of execution:

1) loop body

2) repeat from step 1 until entire array or collection has been traversed

Example – Integer Array

int [] intArray = {1, 3, 5, 7, 9};
for(int currentValue : intArray) {
  System.out.println(currentValue);
}

The currentValue variable holds the current value being looped over in the intArray array. Notice there’s no explicit step value – it’s always an increment by 1.

The colon can be thought of to mean “in”. So the enhanced for loop declaration states: loop over intArray and store the current array int value in the currentValue variable.

Output:

1
3
5
7
9

Example – String Array

We can use the for-each loop to iterate over an array of strings. The loop declaration states: loop over myStrings String array and store the current String value in the currentString variable.

String [] myStrings  = {
  "alpha",
  "beta",
  "gamma",
  "delta"
};

for(String currentString : myStrings) {
  System.out.println(currentString);
}

Output:

alpha
beta
gamma
delta

Example – List

The enhanced for loop can also be used to iterate over a java.util.List as follows:

List<String> myList = new ArrayList<String>();
myList.add("alpha");
myList.add("beta");
myList.add("gamma");
myList.add("delta");

for(String currentItem : myList) {
  System.out.println(currentItem);
}

The loop declaration states: loop over myList List of Strings and store the current List value in the currentItem variable.

Output:

alpha
beta
gamma
delta

Example – Set

The enhanced for loop can also be used to iterate over a java.util.Set as follows:

Set<String> mySet = new HashSet<String>();
mySet.add("alpha");
mySet.add("alpha");
mySet.add("beta");
mySet.add("gamma");
mySet.add("gamma");
mySet.add("delta");

for(String currentItem : mySet) {
  System.out.println(currentItem);
}

The loop declaration states: loop over mySet Set of Strings and store the current Set value in the currentItem variable. Notice that since this is a Set, duplicate String values are not stored.

Output:

alpha
delta
beta
gamma

Source: Loops in Java – Ultimate Guide


P
Peter Mortensen

As so many good answers said, an object must implement the Iterable interface if it wants to use a for-each loop.

I'll post a simple example and try to explain in a different way how a for-each loop works.

The for-each loop example:

public class ForEachTest {

    public static void main(String[] args) {

        List<String> list = new ArrayList<String>();
        list.add("111");
        list.add("222");

        for (String str : list) {
            System.out.println(str);
        }
    }
}

Then, if we use javap to decompile this class, we will get this bytecode sample:

public static void main(java.lang.String[]);
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=4, args_size=1
         0: new           #16                 // class java/util/ArrayList
         3: dup
         4: invokespecial #18                 // Method java/util/ArrayList."<init>":()V
         7: astore_1
         8: aload_1
         9: ldc           #19                 // String 111
        11: invokeinterface #21,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
        16: pop
        17: aload_1
        18: ldc           #27                 // String 222
        20: invokeinterface #21,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
        25: pop
        26: aload_1
        27: invokeinterface #29,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;

As we can see from the last line of the sample, the compiler will automatically convert the use of for-each keyword to the use of an Iterator at compile time. That may explain why object, which doesn't implement the Iterable interface, will throw an Exception when it tries to use the for-each loop.


P
Peter Mortensen

As many of other answers correctly state, the for each loop is just syntactic sugar over the same old for loop and the compiler translates it to the same old for loop.

javac (OpenJDK) has a switch, -XD-printflat, which generates a Java file with all the syntactic sugar removed. The complete command looks like this:

javac -XD-printflat -d src/ MyFile.java

//-d is used to specify the directory for output java file

So let’s remove the syntactical sugar

To answer this question, I created a file and wrote two versions of for each, one with array and another with a list. My Java file looked like this:

import java.util.*;

public class Temp{

    private static void forEachArray(){
        int[] arr = new int[]{1,2,3,4,5};
        for(int i: arr){
            System.out.print(i);
        }
    }

    private static void forEachList(){
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        for(Integer i: list){
            System.out.print(i);
        }
    }
}

When I compiled this file with above switch, I got the following output.

import java.util.*;

public class Temp {

    public Temp() {
        super();
    }

    private static void forEachArray() {
        int[] arr = new int[]{1, 2, 3, 4, 5};
        for (/*synthetic*/ int[] arr$ = arr, len$ = arr$.length, i$ = 0; i$ < len$; ++i$) {
            int i = arr$[i$];
            {
                System.out.print(i);
            }
        }
    }

    private static void forEachList() {
        List list = Arrays.asList(new Integer[]{Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4), Integer.valueOf(5)});
        for (/*synthetic*/ Iterator i$ = list.iterator(); i$.hasNext(); ) {
            Integer i = (Integer)i$.next();
            {
                System.out.print(i);
            }
        }
    }
}

You can see that along with the other syntactic sugar (Autoboxing), for each loops got changed to simple loops.


P
Peter Mortensen
public static Boolean Add_Tag(int totalsize)
{
    List<String> fullst = new ArrayList<String>();
    for(int k=0; k<totalsize; k++)
    {
        fullst.addAll();
    }
}

I don't think we need this answer. See this comment.
T
TheArchon

The Java for-each idiom can only be applied to arrays or objects of type *Iterable. This idiom is implicit as it truly backed by an Iterator. The Iterator is programmed by the programmer and often uses an integer index or a node (depending on the data structure) to keep track of its position. On paper it is slower than a regular for-loop, a least for "linear" structures like arrays and Lists but it provides greater abstraction.


-1: this is way less readable (generally): even your own example (the first one) is wrong, as it leaves out the first element in the array.
R
Rei Brown

This looks crazy but hey it works

List<String> someList = new ArrayList<>(); //has content
someList.forEach(System.out::println);

This works. Magic


This requires Java 1.8 +
doesn't even remotely answer the question
An explanation would be in order.
n
nabayram

I think this will work:

for (Iterator<String> i = someList.iterator(); i.hasNext(); ) {
   String x = i.next();
   System.out.println(x);
}

Please explain why your code and try to answer the question "How does the Java 'for each' loop work?"
P
Peter Mortensen

The code would be:

import java.util.ArrayList;
import java.util.List;

public class ForLoopDemo {

  public static void main(String[] args) {

    List<String> someList = new ArrayList<String>();

    someList.add("monkey");
    someList.add("donkey");
    someList.add("skeleton key");

    // Iteration using For Each loop
    System.out.println("Iteration using a For Each loop:");
    for (String item : someList) {
      System.out.println(item);
    }

    // Iteration using a normal For loop
    System.out.println("\nIteration using normal For loop: ");
    for (int index = 0; index < someList.size(); index++) {
      System.out.println(someList.get(index));
    }
  }
}

1) This is incorrect. The direct equivalent to a for-each on an iterable (such as a list) uses an iterator. That's what the JLS says. Look it up. 2) We don't need more answers to this question. Please don't waste everyone's time by adding yet another answer to old questions. Find new questions that you can answer.
P
Peter Mortensen

Using forEach:

int[] numbers = {1,2,3,4,5};

Arrays.stream(numbers).forEach(System.out::println);

Response:

1
2
3
4
5

The process finished with exit code 0

PS: You need a Array (int[] numbers), and import java.util.Arrays;


As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.