Java_Logo There are three common ways to iterate through a Collection in Java using either while(), for() or for-each(). While each technique will produce more or less the same results, the for-each construct is the most elegant and easy to read and write. It doesn’t require an Iterator and is thus more compact and probably more efficient. It is only available since Java 5 so you can’t use it if you are restrained to Java 1.4 or earlier. Following, the three common methods for iterating through a Collection are presented, first using a while loop, then a for loop, and finally a for-each loop. The Collection in this example is a simple ArrayList of Strings.





The result for each method should look like this:

Are you interested in the cutting edge of AI processors? Read how to build the Ex-Machina Wetware Brain and learn about the Knowm Technology Stack.

Related Posts

Subscribe To Our Newsletter

Join our low volume mailing list to receive the latest news and updates from our team.

Leave a Comment

Knowm 32X32 Crossbar

Knowm Newsletter

Are you ready for memristor AI processors? With our newsletter, you will be.