Matt RaibleMatt Raible is a Web Developer and Java Champion. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

How do you iterate your lists?

This seems to be a matter of personal preference, but I'm interested in hearing how other programmers do their loops. Here's how I do it most often:

List users = dao.getUsers();
for (int i=0; i < users.size(); i++) {
    User user = (User) users.get(i);
    // do something with user
}

I've seen others do it with an Iterator, and I've done it this way myself, but for some reason I prefer the for loop:

List users = dao.getUsers();
Iterator userIter = users.iterator();
while (userIter.hasNext()) {
    User user = (User) userIter.next();
    // do something with user
}

I usually add a null check as part of the loop as well. They both seem pretty much the same to me - is one more performant than the other? John Watkinson points out that in JDK 1.5, the for loop will be even easier:

List users = dao.getUsers();
for (User user : list) {
    // do something with user

At least that's how I interpreted it - please correct me if I'm wrong. Also, enlighten me if I should be using an Iterator - or at least let me know what advantages it has over a for loop.

Posted in General at May 12 2003, 04:11:37 PM MDT 6 Comments
Comments:

for (Iterator iterator = dao.getUsers().iterator(); iterator.hasNext();) {
     User user = (User) iterator.next();
     // do something with the user
}

There's one main reason why this is how I iterate collections. It doesn't reveal any internal implementation details, and it doesn't assume anything about the best method for iterating the collection. I don't do much work with the List interface often. Only when I need the features it provides that the Collection interface lacks. This way, I can work with List and Set collections without adjusting my code. Let the collection worry about how to iterate over its elements. that's not my job.

The other reason is that it doesn't pollute your namespace outside of your loop. But that's ancillary.
Jesse

Posted by Jesse Blomberg on May 12, 2003 at 10:32 PM MDT #

If you use an iterator, and your loop or another thread changes the list (not through the iterator) while iterating through it, an exeption gets thrown.

Posted by Paul Rivers on May 12, 2003 at 10:43 PM MDT #

Unless your sure that the underlying implementation uses an Array based implementation (Arraylist, Vector) stay away for using .get() methods. For LinkedList for examples .get() operations run in linear time and not constant time like its the case for Vectors and ArrayLists.

Posted by Kasper Nielsen on May 12, 2003 at 11:31 PM MDT #

I used to do it like your first example, using a for loop to directly access the item by its index, even though I knew I should have been using an Iterator to be completely independant from the underlying implementation. However after few pointers given to me in response to my question on the topic I decided I liked the solution that Jesse explained in his comment. I think it nicely embeds the use of an Iterator into the cleanliness of a for loop. To me, it doesn't look that bad either.

Posted by Ryan on May 14, 2003 at 02:09 AM MDT #

I prefer the variant using an iterator inside a for-loop. If I know for sure, that a method will only need sequential access to a collection, I will take an iterator instead of the collection as parameter.
This came in very handy, when we had to operate on very large data sets. We implemented an iterator on top of an ObjectInputStream and thus were able to put the data sets into temporary files.
When our server was equipped with more RAM, we could switch back to memory-based collections without much ado.

Posted by Fokko Degenaar on May 14, 2003 at 07:11 AM MDT #

Normally it's better to cache the size into it's own variable and use that instead of repeatably calling size() on each iteration.

Posted by Robert Nicholson on May 16, 2003 at 12:58 PM MDT #

Post a Comment:
  • HTML Syntax: Allowed