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 499121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:57:11+00:00 2026-05-13T05:57:11+00:00

Is there any performance testing results available in comparing traditional for loop vs Iterator

  • 0

Is there any performance testing results available in comparing traditional for loop vs Iterator while traversing a ArrayList,HashMap and other collections?

Or simply why should I use Iterator over for loop or vice versa?

  • 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-13T05:57:12+00:00Added an answer on May 13, 2026 at 5:57 am

    Assuming this is what you meant:

    // traditional for loop
    for (int i = 0; i < collection.size(); i++) {
      T obj = collection.get(i);
      // snip
    }
    
    // using iterator
    Iterator<T> iter = collection.iterator();
    while (iter.hasNext()) {
      T obj = iter.next();
      // snip
    }
    
    // using iterator internally (confirm it yourself using javap -c)
    for (T obj : collection) {
       // snip
    }
    

    Iterator is faster for collections with no random access (e.g. TreeSet, HashMap, LinkedList). For arrays and ArrayLists, performance differences should be negligible.

    Edit: I believe that micro-benchmarking is root of pretty much evil, just like early optimization. But then again, I think it’s good to have a feeling for the implications of such quite trivial things. Hence I’ve run a small test:

    • iterate over a LinkedList and an ArrayList respecively
    • with 100,000 “random” strings
    • summing up their length (just something to avoid that compiler optimizes away the whole loop)
    • using all 3 loop styles (iterator, for each, for with counter)

    Results are similar for all but “for with counter” with LinkedList. All the other five took less than 20 milliseconds to iterate over the whole list. Using list.get(i) on a LinkedList 100,000 times took more than 2 minutes (!) to complete (60,000 times slower). Wow! 🙂 Hence it’s best to use an iterator (explicitly or implicitly using for each), especially if you don’t know what type and size of list your dealing with.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Add them yourself in a date components object. When you… May 13, 2026 at 10:09 am
  • Editorial Team
    Editorial Team added an answer This particular question has come up on multiple occasions and… May 13, 2026 at 10:09 am
  • Editorial Team
    Editorial Team added an answer Asynchronously, by default. If you need them to be one-after-the-other,… May 13, 2026 at 10:09 am

Related Questions

I am new to C++ style casts and I am worried that using C++
While doing performance testing on windows, i usually use perfmon. But when i put
I have some basic questions around understanding fundamentals of Performance testing. I know that
I'm in the process of developing my first major project. It's a light-weight content
Summary jquery is used to retrieve search results via the get() call. When rendering

Trending Tags

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

Top Members

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.