DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Which For Loop?

There are three types of for loops out there—for loops, for-each loops, and Java 8's lambda-friendly for-each loops. See which you should use—and when.

Dan Newton user avatar by
Dan Newton
·
Jan. 24, 17 · Tutorial
Like (22)
Save
Tweet
Share
11.21K Views

Join the DZone community and get the full member experience.

Join For Free

Java 8 introduced another type of for loop. This gave us the third way to use them. You would think that releasing another way to use the loop would suggest that it must be better than the others. But that is not the case. Each one has subtle differences, meaning you should have a little think before you decide which one to use.

Types of for loops:

  • Normal for loop
  • For-each loop
  • Java 8 for-each loop

Normal For Loop

Now I’m just going to come straight out and say this: These loops look ugly and I try to stay away from them as much as possible. Anyway, now that I have said that, let's get back to explaining what they are. This type of loop uses a counter to iterate through the elements of a list or array. The easiest way to explain it is through an example.

List<Integer> list = Arrays.asList(1,2,3,4,5,6);
for(int index=0; index<list.size(); index++) {
    System.out.println("I have read the number: " + list.get(index));
}

int[] array = new int[]{ 1,2,3,4,5,6};
for(int index=0; index<array.length; index++) {
    System.out.println("I have read the number: " + array[index]);
}


The above shows how to iterate through both a list and an array. It does so by increasing the counters value index and retrieving the element at that position using list.get(index) or array[index]. After it has the element it can do whatever it wants with it.

The advantage of using this type of loop is that you have more control over how you read the elements than you do with the other types of loops. If you wanted, you could retrieve the element at position index-1 very easily. Just remember to handle the scenario of index = 0, or you're going to have an IndexOutOfBoundsException on your hands. This advantage of control is also its downside, as you need to manually retrieve the elements, which makes the code look cluttered. The ugliness of this loop is not found in the other types of loops.

The For-Each Loop

The name of this loop pretty much sums up what it does. For each element in the collection or array, do something with it. The is no need to increment through the list using a counter as that is all done behind the scenes. Simply give the object you want to retrieve and type and name and you are good to go.

List<Integer> list = Arrays.asList(1,2,3,4,5,6);
for(int number : list) {
    System.out.println("I have read the number:" + number);
}

int[] array = new int[]{ 1,2,3,4,5,6};
for(int number : array) {
    System.out.println("I have read the number: " + number);
}

Set<Integer> set = new HashSet<>(Arrays.asList(1,2,3,4,5,6));
for(int number : set) {
    System.out.println("I have read the number: " + number);
}


Look how simple and pretty they are, so much nicer than those ugly loops above. Anyway, the loops will go through the elements and, during each iteration, provide the defined variable a value from the collection or array. The order in which the values are retrieved follows the same order that the elements have in the collection or array. Another advantage of the for-each loop is that it can iterate through the elements of a Set, as it does not have its own get() method.

The Java 8 For-Each Loop

This is the new for loop that has been added with the release of Java 8. It has all the characteristics of the normal for-each loop but uses lambda expressions to define what it does with each element it iterates over from the collection, although this form cannot be used on an array. The example below is pretty self-explanatory.

List<Integer> list = Arrays.asList(1,2,3,4,5,6);
list.forEach(number -> System.out.println("I have read the number:" + number));


This form of for loop can be used instead of the normal for-each loop, as, functionally, they are the same, but the syntax is different. This form looks nicer for simple lambda expressions and can allow a loop to be defined in a single line if the expression is small enough. When it comes to loops with more statements inside them, it is the user's preference which for-each loop they use, as the benefit of decreasing the amount of code written is no longer there.

So which loop should you use? If you need total control over the elements you read or need to add and remove elements from the list, then a normal for loop is the way to go. If you simply need to perform some statements on elements of the list and don’t need to add or remove elements, then both the normal for-each and Java 8 for-each loops will suffice. Determining which for-each loop to use is up to you. Personally, for singular or such smaller statements, I would use the Java 8 for-each loop, but for loops with longer statements inside, then the good old normal for-each will be my loop of choice.

Element

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Java Version Are You Running? Let’s Take a Look Under the Hood of the JDK!
  • Distributed SQL: An Alternative to Database Sharding
  • Using QuestDB to Collect Infrastructure Metrics
  • PostgreSQL: Bulk Loading Data With Node.js and Sequelize

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: