Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 582791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:43:29+00:00 2026-05-13T14:43:29+00:00

Which is the most efficient way to traverse a collection? List<Integer> a = new

  • 0

Which is the most efficient way to traverse a collection?

List<Integer>  a = new ArrayList<Integer>();
for (Integer integer : a) {
  integer.toString();
}

or

List<Integer>  a = new ArrayList<Integer>();
for (Iterator iterator = a.iterator(); iterator.hasNext();) {
   Integer integer = (Integer) iterator.next();
   integer.toString();
}

Please note, that this is not an exact duplicate of this, this, this, or this, although one of the answers to the last question comes close. The reason that this is not a dupe, is that most of these are comparing loops where you call get(i) inside the loop, rather than using the iterator.

As suggested on Meta I will be posting my answer to this question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:43:29+00:00Added an answer on May 13, 2026 at 2:43 pm

    If you are just wandering over the collection to read all of the values, then there is no difference between using an iterator or the new for loop syntax, as the new syntax just uses the iterator underwater.

    If however, you mean by loop the old “c-style” loop:

    for(int i=0; i<list.size(); i++) {
       Object o = list.get(i);
    }
    

    Then the new for loop, or iterator, can be a lot more efficient, depending on the underlying data structure. The reason for this is that for some data structures, get(i) is an O(n) operation, which makes the loop an O(n2) operation. A traditional linked list is an example of such a data structure. All iterators have as a fundamental requirement that next() should be an O(1) operation, making the loop O(n).

    To verify that the iterator is used underwater by the new for loop syntax, compare the generated bytecodes from the following two Java snippets. First the for loop:

    List<Integer>  a = new ArrayList<Integer>();
    for (Integer integer : a)
    {
      integer.toString();
    }
    // Byte code
     ALOAD 1
     INVOKEINTERFACE java/util/List.iterator()Ljava/util/Iterator;
     ASTORE 3
     GOTO L2
    L3
     ALOAD 3
     INVOKEINTERFACE java/util/Iterator.next()Ljava/lang/Object;
     CHECKCAST java/lang/Integer
     ASTORE 2 
     ALOAD 2
     INVOKEVIRTUAL java/lang/Integer.toString()Ljava/lang/String;
     POP
    L2
     ALOAD 3
     INVOKEINTERFACE java/util/Iterator.hasNext()Z
     IFNE L3
    

    And second, the iterator:

    List<Integer>  a = new ArrayList<Integer>();
    for (Iterator iterator = a.iterator(); iterator.hasNext();)
    {
      Integer integer = (Integer) iterator.next();
      integer.toString();
    }
    // Bytecode:
     ALOAD 1
     INVOKEINTERFACE java/util/List.iterator()Ljava/util/Iterator;
     ASTORE 2
     GOTO L7
    L8
     ALOAD 2
     INVOKEINTERFACE java/util/Iterator.next()Ljava/lang/Object;
     CHECKCAST java/lang/Integer
     ASTORE 3
     ALOAD 3
     INVOKEVIRTUAL java/lang/Integer.toString()Ljava/lang/String;
     POP
    L7
     ALOAD 2
     INVOKEINTERFACE java/util/Iterator.hasNext()Z
     IFNE L8
    

    As you can see, the generated byte code is effectively identical, so there is no performance penalty to using either form. Therefore, you should choose the form of loop that is most aesthetically appealing to you, for most people that will be the for-each loop, as that has less boilerplate code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 504k
  • Answers 504k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If it's an empty anchor shouldn't it be < a… May 16, 2026 at 3:15 pm
  • Editorial Team
    Editorial Team added an answer a) How developers of a Plugin system can ensure that… May 16, 2026 at 3:15 pm
  • Editorial Team
    Editorial Team added an answer What you need is VaryByControl. See Vary by control properties… May 16, 2026 at 3:15 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I don't know which is the most efficient way of organizing images downloaded from
Which is the most reliable way to check if a character array is empty?
which is the best way to write a bidimensional hashmap efficiently in Java? Just
Ok I have been trying every which way to figure this out. I need
I need to perform some operations on all folders within a file share which
I've written the following routine to manually traverse through a directory and calculate its
I am looking for a somewhat detailed explanation of which 3D Engine for Actionscript3
I have a list filter = ['a', 'b', 'c']. I need to frame the
My code processes a huge number of values and I'm looking for an efficient
I have a data.frame x in R for which dim(x) = (m,n) and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.