Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

core java collection

 
vivek shankare gowda
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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?
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • 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.
 
Sunil Sunny
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.)
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other cases you should use subList.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic