From the course: Java 8 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Looping through collections with iterators and for-each

Looping through collections with iterators and for-each - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Looping through collections with iterators and for-each

- Java gives you a number of ways of looping through or iterating through collections of data, either ordered or unordered data. I'm going to demonstrate some of these techniques in the project "IterateCollections". I'm starting with an ArrayList declared at line 13 and it contains three items, and in line 19 I'm outputting that list using simple output passing the object to print line, and that cause the two-string method of the object. When I run the application I get the data printed out with the standard formatting. Now I'm going to describe how to use something called an Iterator object, this used to be the most common way of looping through a collection. You create something called an Iterator object and there's an interface available for this, and notice up at the top that now I have an import statement that says "java.util.*". IntelliJ IDEA has decided that I'm using so many interfaces and classes from this package that instead of importing them individually it's just going to…

Contents