• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which is fastest way to iterate through a collection?  RSS feed

 
narendra nath
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can iterate thru a collection using For loop or While loop or even iterator.

What are the pros and cons of using each way to iterate.

thanks
dra
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Worrying about what is fastest is the wrong approach. you should instead work towards writing the simplest, cleanest code you can.

Generally speaking, if you know exactly how many times you want to do something, you would use a for loop. If you don't, you'd use a while loop. You would use a for-each loop if you have a collection where that would work.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Worrying about what is fastest is the wrong approach. You should instead work towards writing the simplest, cleanest code you can.


That's important enough to immortalize:



  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!