How to iterate a List in Java

This tutorial shows several ways for iterating a List in Java.

1- Typical For loop

Using a typical For loop, you can iterate a List as the following:

2- Enhanced For loop

Since JDK 5.0, you can iterate a List using an enhanced For loop which works as the following:

3- Typical While loop

Using a typical while loop, you can iterate a List as the following:

4- Iterator

Collections in Java can be iterated through Iterator as the following:

5- Java 8

With Java 8, you can iterate a List in one line using forEach() method as the following:

 

Hussein Terek

Founder of programmergate.com, I have a passion in software engineering and everything related to java environment.

You may also like...

Leave a Reply

avatar