Iterable interface: while loop and for loop : Iterable Interface « Collections « Java Tutorial






import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class MainClass {
  public static void main(String[] args) {
    List list = Arrays.asList("A", "B", "C", "D");
     Iterator iterator = list.iterator();
     while (iterator.hasNext () ) {
         String element = (String) iterator.next ();
         System.out.println(element);
     }

  }
}
A
B
C
D

This is identical to:

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class MainClass {
  public static void main(String[] args) {
    List list = Arrays.asList("A", "B", "C", "D");

    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
      String element = (String) iterator.next();
      System.out.println(element);
    }

  }
}
A
B
C
D








9.36.Iterable Interface
9.36.1.Using an Iterator
9.36.2.Iterable interface: while loop and for loop
9.36.3.'for' statement for Iterable object in JDK 5 has been enhanced.
9.36.4.Creating Iterable Objects: using a for-each for loop on an Iterable object