Use Iterable to loop through a list in Java

Description

The following code shows how to use Iterable to loop through a list.

Example


//from w w w .  j  a  va 2  s  . c  o  m
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Main {
  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);
     }

  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Java Collection »




Java ArrayList
Java Collection
Java Comparable
Java Comparator
Java HashMap
Java HashSet
Java Iterator
Java LinkedHashMap
Java LinkedHashSet
Java LinkedList
Java List
Java ListIterator
Java Map
Queue
Java Set
Stack
Java TreeMap
TreeSet