Loop an Iterable in Java

If you use some Java API and it returns an Iterable, such as Iterable, you may want to loop this iterable.

The method is pretty simple as follows:

Iterable<String> result = ... //result returned from some method.
for(String s: result){
	System.out.println(s);
}

If you use hasNext(), you can also do the following:

Iterable<String> result = ... //result returned from some method.
Iterator<String> iter = result.iterator();
while(iter.hasNext()){
	System.out.println(iter.next());
}

Iterable is an interface in java.lang, it defines that a method iterator() which returns an Iterator.

Category >> Basics >> Collections  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>