• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

core java collection

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if there are plenty of records to iterate,which collection is best to use to iterate ? like array list ,queue..and why?
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each is different (and in some cases common).

Some depend on the ordering, or collating.. for example sorting a Collection of People by Age,
others simple such as sorting by insertion data or timestamp.

Some are key,value bases, some are not.

Some allow insertions/deletions before/after other elements in the Collection, maintaining ordering.

Hint: Try to use real world examples, for instance if you wanted to sort a Collection of your relatives by Age, how would you design that?

Why I interview candidates, I use a deck of cards (standard 52 cards, 4 suits, 13 values, no jokers) and ask them which they would use.

Pat.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you are just a beginner you can use array list and use the for loop to iterate as Array List maintains indexes with no overhead. And also for loop works faster than iterator.

But as william said different collections for different purpose
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sunil Sunny wrote:As you are just a beginner you can use array list and use the for loop to iterate as Array List maintains indexes with no overhead. And also for loop works faster than iterator.



No, no, a thousand times no.

When iterating a collection, always use an Iterator or foreach loop (which is itself just syntactic sugar for an Iterator). Do not iterate using get(i). If get(i) is any faster, it will be by a tiny amount that will never be noticed in the context of an actual application, and it's a bad habit to get into, since, unlike Iterator/foreach, it's only viable for one particular collection and not something that can be used in the general case.

Please don't encourage these kinds of pointless microoptimizatoins.

(Also, note that the choice isn't for loop vs. Iterator. It's get(i) vs. Iterator. A very common idiom (one I used all the time before foreach came along) uses a for loop with an Iterator instead of a while loop.)
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:When iterating a collection, always use an Iterator or foreach loop (which is itself just syntactic sugar for an Iterator). Do not iterate using get(i). If get(i) is any faster, it will be by a tiny amount that will never be noticed in the context of an actual application, and it's a bad habit to get into, since, unlike Iterator/foreach, it's only viable for one particular collection and not something that can be used in the general case.


It could easily be the case that we are iterating with i because we need to know the value of i.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Deems wrote:

Jeff Verdegan wrote:When iterating a collection, always use an Iterator or foreach loop (which is itself just syntactic sugar for an Iterator). Do not iterate using get(i). If get(i) is any faster, it will be by a tiny amount that will never be noticed in the context of an actual application, and it's a bad habit to get into, since, unlike Iterator/foreach, it's only viable for one particular collection and not something that can be used in the general case.


It could easily be the case that we are iterating with i because we need to know the value of i.



Then add a counter variable. There's never a good reason for iterating with get(i), IMHO. It's a total hack.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Dennis Deems wrote:It could easily be the case that we are iterating with i because we need to know the value of i.



Then add a counter variable. There's never a good reason for iterating with get(i), IMHO. It's a total hack.


"Total hack"?? Really, I think this is an extreme position.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Deems wrote:

Jeff Verdegan wrote:

Dennis Deems wrote:It could easily be the case that we are iterating with i because we need to know the value of i.



Then add a counter variable. There's never a good reason for iterating with get(i), IMHO. It's a total hack.


"Total hack"?? Really, I think this is an extreme position.



To each his own I guess.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using get(i) works nicely if you need to iterate only part of a List. Obviously it is best to restrict its use to Lists implementing RandomAccess.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other cases you should use subList.
 
reply
    Bookmark Topic Watch Topic
  • New Topic