How to iterate through Java List? Five (5) ways to Iterate Through Loop in Java
here are 5 ways you can iterate through List.
1.For Loop 2.Advanced For Loop 3.Iterator 4.While Loop 5.Collections’s stream() util (Java8) Java Example:
You need JDK 8 to run below program as point-5 above uses stream() util.
void java.util.stream.Stream.forEach(Consumer<? super String> action) performs an action for each element of this stream.
package crunchify.com.tutorial;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author Crunchify.com
*/
public class CrunchifyIterateThroughList {
public static void main(String[] argv) {
// create list
List<String> crunchifyList = new ArrayList<String>();
// add 4 different values to list
crunchifyList.add("eBay");
crunchifyList.add("Paypal");
crunchifyList.add("Google");
crunchifyList.add("Yahoo");
// iterate via "for loop"
System.out.println("==> For Loop Example.");
for (int i = 0; i < crunchifyList.size(); i++) {
System.out.println(crunchifyList.get(i));
}
// iterate via "New way to loop"
System.out.println("\n==> Advance For Loop Example..");
for (String temp : crunchifyList) {
System.out.println(temp);
}
// iterate via "iterator loop"
System.out.println("\n==> Iterator Example...");
Iterator<String> crunchifyIterator = crunchifyList.iterator();
while (crunchifyIterator.hasNext()) {
System.out.println(crunchifyIterator.next());
}
// iterate via "while loop"
System.out.println("\n==> While Loop Example....");
int i = 0;
while (i < crunchifyList.size()) {
System.out.println(crunchifyList.get(i));
i++;
}
// collection stream() util: Returns a sequential Stream with this collection as its source
System.out.println("\n==> collection stream() util....");
crunchifyList.forEach((temp) -> {
System.out.println(temp);
});
}
}